README.md 6.52 KB
Newer Older
PanZezhong1725's avatar
PanZezhong1725 committed
1
# InfiniLM
PanZezhong's avatar
PanZezhong committed
2
3
4
5
6
7
8

本项目是基于 [`InfiniCore`](https://github.com/InfiniTensor/InfiniCore) 的推理引擎。

## 使用方式

- 编译并安装 `InfiniCore` 。注意根据提示设置好 `INFINI_ROOT` 环境变量(默认为 `$HOME/.infini`)。

PanZezhong1725's avatar
PanZezhong1725 committed
9
- 编译并安装 `InfiniLM`
PanZezhong's avatar
PanZezhong committed
10
11
12
13
14
15
16
17

```bash
xmake && xmake install
```

- 运行模型推理测试

```bash
xgqdut2016's avatar
xgqdut2016 committed
18
python scripts/jiuge.py [--cpu | --nvidia | --qy | --cambricon | --ascend | --metax | --moore | --iluvatar | --kunlun | --hygon] path/to/model_dir [n_device]
PanZezhong's avatar
PanZezhong committed
19
20
21
22
23
```

- 部署模型推理服务

```bash
xgqdut2016's avatar
xgqdut2016 committed
24
python scripts/launch_server.py --model-path MODEL_PATH [-h] [--dev {cpu,nvidia,qy, cambricon,ascend,metax,moore,iluvatar,kunlun,hygon}] [--ndev NDEV] [--max-batch MAX_BATCH] [--max-tokens MAX_TOKENS]
PanZezhong's avatar
PanZezhong committed
25
26
27
28
29
```

- 测试模型推理服务性能

```bash
30
python scripts/test_perf.py
PanZezhong's avatar
PanZezhong committed
31
32
33
34
35
36
```

- 使用推理服务测试模型困惑度(Perplexity)

```bash
python scripts/test_ppl.py --model-path MODEL_PATH [--ndev NDEV] [--max-batch MAX_BATCH] [--max-tokens MAX_TOKENS]
PanZezhong's avatar
PanZezhong committed
37
```
38
39

## 使用方式(新版)
40
41
#### 一、编译并安装 `InfiniCore`
编译并安装 `InfiniCore`, 详情见 InfiniCore的 [`README`](https://github.com/InfiniTensor/InfiniCore) :
42

43
44
45
46
47
- 注意根据提示设置好 `INFINI_ROOT` 环境变量(默认为 `$HOME/.infini`
- 根据硬件平台,选择 xmake 构建配置
- 编译安装InfiniCore
- 安装 C++ 库
- 安装 Python 包
48

49

50
51
#### 二、编译并安装  `InfiniLM`
  - 克隆项目
52

53
    由于仓库中含有子模块,所以在克隆时请添加 `--recursive` 或 `--recurse-submodules`,如:
54

55
    ```shell
pengcheng888's avatar
pengcheng888 committed
56
    git clone --recursive https://github.com/InfiniTensor/InfiniLM.git
57
58
59
    ```

    或者在普通克隆后进行更新:
60

61
    ```shell
62
    git submodule update --init --recursive
63
64
    ```

65

66
67
68
69
70
71
  - 选择是否使用九齿计算路径,默认为false,即不依赖九齿算子
    ```bash
      xmake f --ninetoothed= [true | false] -cv
    ```


72
  - 安装 InfiniLM Python 包
73
74
75
    ```bash
      pip install -e .
    ```
76

77
  - 单次推理测试
78
    - llama示例
79
    ```bash
80
    python examples/llama.py [--cpu | --nvidia | --qy | --metax | --moore | --iluvatar | --ali] --model_path=<path/to/model_dir>
81
82
83
84
85
    ```
    - 例如:
    ```bash
    python examples/llama.py --nvidia --model_path=/models/TinyLlama-1.1B-Chat-v1.0
    ```
86
87
88
  - 分布式推理测试
      - 9g示例
      ```bash
89
    python examples/jiuge.py [---nvidia] --model_path=<path/to/model_dir> --backend=cpp --tp=NDEV --batch_size=MAX_BATCH
90
    ```
91

92
93
    - 例如: 9G7B模型,cpp后端,batch_size为16,4卡分布式
    ```bash
94
95
96
    python examples/jiuge.py --nvidia --model_path=/models/9G7B_MHA/ --backend=cpp --tp=4 --batch_size=16
    ```

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

  - 推理服务测试
    - 启动推理服务
      ```bash
      python python/infinilm/server/inference_server.py [--cpu | --nvidia | --metax | --moore | --iluvatar | --cambricon] --model_path=<path/to/model_dir> --max_tokens=MAX_TOKENS --max_batch_size=MAX_BATCH --tp=NDEV --temperature=TEMP --top_p=TOP_P --top_k=TOP_K --host=HOST --port=PORT
      ```
    
    - 单卡示例:
      ```bash
      CUDA_VISIBLE_DEVICES=0 python python/infinilm/server/inference_server.py --nvidia --model_path=/models/9G7B_MHA/ --max_tokens=100 --max_batch_size=32 --tp=1 --temperature=1.0 --top_p=0.8 --top_k=1
      ```
    
    - 多卡分布式示例:
      ```bash
      CUDA_VISIBLE_DEVICES=0,1,2,3 python python/infinilm/server/inference_server.py --nvidia --model_path=/models/9G7B_MHA/ --max_tokens=100 --max_batch_size=32 --tp=4 --temperature=1.0 --top_p=0.8 --top_k=1
      ```
    
    - 测试推理服务性能:
      ```bash
      python scripts/test_perf.py --verbose
      ```

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
  - 运行推理基准测试(C-Eval/MMLU)

    ```bash
    python test/bench/test_benchmark.py [--cpu | --nvidia | --cambricon | --ascend | --metax | --moore | --iluvatar | --kunlun | --hygon] <path/to/model_dir> --bench {ceval|mmlu} [--backend cpp] [--ndev N] [--subject SUBJECT] [--num_samples N] [--max_new_tokens N] [--output_csv PATH] [--cache_dir PATH]
    ```

    - 参数说明:
      - `--subject`: 指定科目,支持单个科目、多个科目(逗号分隔)或 `all`(默认值,加载全部科目)
      - `--output_csv`: 可选,指定CSV输出文件路径。如未指定则不生成CSV文件。CSV包含每个科目的结果和总体结果
      - `--cache_dir`: 可选,指定数据集缓存目录的父目录。应指向包含 `ceval___ceval-exam` 和 `cais___mmlu` 等数据集子目录的父目录(例如 `~/.cache/huggingface/datasets/`)。设置后脚本优先使用本地 CSV(`pandas.read_csv`)离线加载数据,避免 `load_dataset` 的网络请求

    - C-Eval示例:
      - 单个科目:
        ```bash
        python test/bench/test_benchmark.py --nvidia /models/9G7B_MHA --bench ceval --subject middle_school_mathematics --num_samples 100 --backend cpp --ndev 1
        ```
      - 多个科目(逗号分隔):
        ```bash
        python test/bench/test_benchmark.py --nvidia /models/9G7B_MHA --bench ceval --subject middle_school_mathematics,high_school_physics --backend cpp --ndev 1 --output_csv results.csv
        ```
      - 全部科目并输出CSV:
        ```bash
        python test/bench/test_benchmark.py --nvidia /models/9G7B_MHA --bench ceval --subject all --backend cpp --ndev 1 --output_csv results.csv
        ```
      - 使用缓存目录加速加载:
        ```bash
        python test/bench/test_benchmark.py --nvidia /models/9G7B_MHA --bench ceval --subject middle_school_mathematics --backend cpp --ndev 1 --cache_dir ~/.cache/huggingface/datasets/
        ```
        > 注意:`--cache_dir` 应指向包含 `ceval___ceval-exam` 和 `cais___mmlu` 等数据集子目录的父目录,而不是直接指向这些子目录

    - MMLU示例:
      - 单个科目:
        ```bash
        python test/bench/test_benchmark.py --nvidia /models/9G7B_MHA --bench mmlu --subject abstract_algebra --backend cpp --ndev 1
        ```
      - 多个科目(逗号分隔):
        ```bash
        python test/bench/test_benchmark.py --nvidia /models/9G7B_MHA --bench mmlu --subject abstract_algebra,anatomy,astronomy --backend cpp --ndev 1 --output_csv results.csv
        ```
      - 使用缓存目录加速加载:
        ```bash
        python test/bench/test_benchmark.py --nvidia /models/9G7B_MHA --bench mmlu --subject abstract_algebra --backend cpp --ndev 1 --cache_dir ~/.cache/huggingface/datasets/
        ```
        > 注意:`--cache_dir` 应指向包含 `ceval___ceval-exam` 和 `cais___mmlu` 等数据集子目录的父目录,而不是直接指向这些子目录