Metadata-Version: 2.4
Name: dlinfer-ascend
Version: 0.2.5
Summary: DeepLink Inference Extension
Home-page: https://github.com/DeepLink-org/dlinfer
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Intended Audience :: Developers
Classifier: Operating System :: POSIX :: Linux
Requires-Python: >=3.10, <3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: transformers
Requires-Dist: torch<2.9.0,>=2.3.1
Requires-Dist: torch-npu<2.9.0,>=2.3.1
Requires-Dist: torchvision<0.24.0,>=0.18.1
Requires-Dist: importlib-metadata
Requires-Dist: pyyaml
Requires-Dist: decorator
Requires-Dist: attrs
Requires-Dist: psutil
Requires-Dist: absl-py
Requires-Dist: cloudpickle
Requires-Dist: ml-dtypes
Requires-Dist: scipy
Requires-Dist: tornado
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: license-file
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary


# 介绍

dlinfer提供了一套将国产硬件接入大模型推理框架的解决方案。
对上承接大模型推理框架，对下在eager模式下调用各厂商的融合算子，在graph模式下调用厂商的图引擎。
在dlinfer中，我们根据主流大模型推理框架与主流硬件厂商的融合算子粒度，定义了大模型推理的融合算子接口。

这套融合算子接口主要功能：

1. 将对接框架与对接厂商融合算子在适配工程中有效解耦；
2. 同时支持算子模式和图模式；
3. 图模式下的图获取更加精确匹配，提高最终端到端性能；
4. 同时支持LLM推理和VLM推理。

目前，我们正在全力支持LMDeploy适配国产芯片，包括华为，沐曦，寒武纪等。

# 架构介绍

<!-- markdownlint-disable -->
<div align=center>
<img [dlinfer_arch] src="assets/dlinfer_arch.png" width="50%">
</div>
<!-- markdownlint-restore -->

## 组件介绍

- **op interface**：
大模型推理算子接口，对齐了主流推理框架以及各个厂商的融合算子粒度。
  - 算子模式：在pytorch的eager模式下，我们将通过op interface向下分发到厂商kernel。由于各个厂商对于参数的数据排布有不同的偏好，所以在这里我们并不会规定数据排布，但是为了多硬件的统一适配，我们将会统一参数的维度信息。
  - 图模式：在极致性能的驱动下，在一些硬件上的推理场景中需要依靠图模式。我们利用Pytorch2中的Dynamo编译路线，通过统一的大模型推理算子接口，获取较为粗粒度算子的计算图，并将计算图通过IR转换后提供给硬件厂商的图编译器。
- **framework adaptor**：
将大模型推理算子接口加入推理框架中，并且对齐算子接口的参数。
- **kernel adaptor**：
吸收了大模型推理算子接口参数和硬件厂商融合算子参数间的差异。

# 安装方法

## 各平台镜像地址

- Atlas 800T A3:
  `docker pull crpi-4crprmm5baj1v8iv.cn-hangzhou.personal.cr.aliyuncs.com/lmdeploy_dlinfer/ascend:a3-latest`
  （Atlas 800T A3目前只支持Qwen系列的算子模式下运行）

- Atlas 800T A2:
  `docker pull crpi-4crprmm5baj1v8iv.cn-hangzhou.personal.cr.aliyuncs.com/lmdeploy_dlinfer/ascend:a2-latest`

- Atlas 300I Duo:
  `docker pull crpi-4crprmm5baj1v8iv.cn-hangzhou.personal.cr.aliyuncs.com/lmdeploy_dlinfer/ascend:300i-duo-latest`
  （Atlas 300I Duo目前只支持非eager模式）

- 沐曦C500
  `docker pull crpi-4crprmm5baj1v8iv.cn-hangzhou.personal.cr.aliyuncs.com/lmdeploy_dlinfer/maca:latest`

- 寒武纪云端加速卡
  `docker pull crpi-4crprmm5baj1v8iv.cn-hangzhou.personal.cr.aliyuncs.com/lmdeploy_dlinfer/camb:latest`

## pip安装

```shell
pip install dlinfer-ascend
```

目前只有华为的Atlas 800T A2与300I Duo支持pip安装。其他硬件请使用源码安装。

## 源码安装

### 华为Atlas 800T A2/A3/300I Duo

1. 在Atlas 800T A2上依赖torch和torch_npu，运行以下命令安装torch、torch_npu及其依赖。

   ```shell
   pip3 install -r requirements/ascend/full.txt
   ```

2. 完成上述准备工作后，使用如下命令即可安装dlinfer。

   ```shell
   cd /path_to_dlinfer
   # 默认不编译 dicp/AtbGraph
   DEVICE=ascend python3 setup.py develop

   # 若需要编译 dicp（例如使用 atbgraph 后端），请显式开启：
   DLINFER_BUILD_DICP=ON DEVICE=ascend python3 setup.py develop
   ```

### 沐曦C500

1. 沐曦软件栈请自行联系沐曦相关人员。

2. 沐曦版本的dlinfer安装命令如下：

   ```shell
   cd /path_to_dlinfer
   DEVICE=maca python3 setup.py develop
   ```

### 寒武纪云端智能加速卡

1. 寒武纪软件栈请自行联系寒武纪相关人员。

2. 寒武纪版本的dlinfer安装命令如下：

   ```shell
   cd /path_to_dlinfer
   DEVICE=camb python3 setup.py develop
   ```

# 支持模型框架列表

## LMDeploy

<!-- markdownlint-disable MD013 -->
|                |           |      |  Atlas 800T A2   |  Atlas 800T A2   | Atlas 800T A2 | Atlas 800T A2 | Atlas 300I Duo |  Atlas 800T A3   | Maca C500 | Cambricon |
| :------------: | :-------: | :--: | :--------------: | :--------------: | :-----------: | :-----------: | :------------: | :--------------: | :-------: | :-------: |
|     Model      |   Size    | Type | FP16/BF16(eager) | FP16/BF16(graph) |  W8A8(graph)  | W4A16(eager)  |  FP16(graph)   | FP16/BF16(eager) |  BF/FP16  |  BF/FP16  |
|     Llama2     | 7B - 70B  | LLM  |       Yes        |       Yes        |      Yes      |      Yes      |       -        |       Yes        |    Yes    |    Yes    |
|     Llama3     |    8B     | LLM  |       Yes        |       Yes        |      Yes      |      Yes      |      Yes       |       Yes        |    Yes    |    Yes    |
|    Llama3.1    |    8B     | LLM  |       Yes        |       Yes        |      Yes      |      Yes      |      Yes       |       Yes        |    Yes    |    Yes    |
|   InternLM2    | 7B - 20B  | LLM  |       Yes        |       Yes        |      Yes      |      Yes      |      Yes       |       Yes        |    Yes    |    Yes    |
|  InternLM2.5   | 7B - 20B  | LLM  |       Yes        |       Yes        |      Yes      |      Yes      |      Yes       |       Yes        |    Yes    |    Yes    |
|   InternLM3    |    8B     | LLM  |       Yes        |       Yes        |      Yes      |      Yes      |      Yes       |       Yes        |    Yes    |    Yes    |
|    Mixtral     |   8x7B    | LLM  |       Yes        |       Yes        |      No       |      No       |      Yes       |        -         |    Yes    |    Yes    |
|  QWen1.5-MoE   |   A2.7B   | LLM  |       Yes        |        -         |      No       |      No       |       -        |        -         |    Yes    |     -     |
|   QWen2(.5)    |    7B     | LLM  |       Yes        |       Yes        |      Yes      |      Yes      |      Yes       |        -         |    Yes    |    Yes    |
|    QWen2-VL    |  2B, 7B   | MLLM |       Yes        |       Yes        |       -       |       -       |       -        |        -         |    Yes    |    No     |
|   QWen2.5-VL   | 3B - 72B  | MLLM |       Yes        |       Yes        |       -       |       -       |      Yes       |        -         |    Yes    |    No     |
|   QWen2-MoE    |  A14.57B  | LLM  |       Yes        |        -         |      No       |      No       |       -        |        -         |    Yes    |     -     |
|     QWen3      | 0.6B-235B | LLM  |       Yes        |       Yes        |      No       |      No       |      Yes       |       Yes        |    Yes    |    Yes    |
|  DeepSeek-V2   |    16B    | LLM  |        No        |       Yes        |      No       |      No       |       -        |        -         |     -     |     -     |
| InternVL(v1.5) |  2B-26B   | MLLM |       Yes        |        -         |      Yes      |      Yes      |       -        |        -         |    Yes    |     -     |
|   InternVL2    |  1B-40B   | MLLM |       Yes        |       Yes        |      Yes      |      Yes      |      Yes       |        -         |    Yes    |    Yes    |
|  InternVL2.5   |  1B-78B   | MLLM |       Yes        |       Yes        |      Yes      |      Yes      |      Yes       |        -         |    Yes    |    Yes    |
|   InternVL3    |  1B-78B   | MLLM |       Yes        |       Yes        |      Yes      |      Yes      |      Yes       |        -         |    Yes    |    Yes    |
|  CogVLM2-chat  |    19B    | MLLM |       Yes        |        No        |       -       |       -       |       -        |        -         |    Yes    |     -     |
|     GLM4V      |    9B     | MLLM |       Yes        |        No        |       -       |       -       |       -        |        -         |     -     |     -     |
<!-- markdownlint-enable MD013 -->

‘Yes’代表测试通过，‘No’代表不支持，‘-’代表未测试

### 使用LMDeploy

LMDeploy安装：

```shell
cd /path_to_lmdeploy
# 华为
LMDEPLOY_TARGET_DEVICE=ascend pip3 install -e .
# 沐曦
LMDEPLOY_TARGET_DEVICE=maca   pip3 install -e .
# 寒武纪
LMDEPLOY_TARGET_DEVICE=camb   pip3 install -e .
```

只需要指定pytorch engine后端为ascend/maca/camb，不需要其他任何修改即可。详细可参考lmdeploy文档。

> [!CAUTION]
> 寒武纪环境下必须把`PytorchEnginConfig`中的`block_size`设为`16`。

示例代码如下：

```python
import lmdeploy
from lmdeploy import PytorchEngineConfig
pipe = lmdeploy.pipeline("/path_to_model",
               backend_config = PytorchEngineConfig(tp=1,
               cache_max_entry_count=0.4, device_type="ascend", eager_mode=True))
question = ["Shanghai is", "Please introduce China", "How are you?"]
response = pipe(question, request_output_len=256, do_preprocess=False)
for idx, r in enumerate(response):
    print(f"Q: {question[idx]}")
    print(f"A: {r.text}")
    print()
```

> [!TIP]
> 图模式已经支持除了昇腾A3之外的所有硬件。

用户可以在离线模式下设定`PytorchEngineConfig`中的`eager_mode=False`来开启图模式，或者设定`eager_mode=True`来关闭图模式。
在线模式下默认开启图模式，请添加`--eager-mode`来关闭图模式。

> \[!IMPORTANT\]
> 目前寒武纪加速卡上启动多卡推理需要手动启动ray。

下面是一个2卡的例子：

```shell
 export MLU_VISIBLE_DEVICES=0,1
 ray start --head --resources='{"MLU": 2}'
```
