README.md 3.01 KB
Newer Older
1
2
3
4
5
6
7
8
# Generative Pre-Training2(GPT2)

## 模型介绍
GPT2模型:第二代生成式预训练模型(Generative Pre-Training2)。

## 模型结构
GPT2主要使用Transformer的Decoder模块为特征提取器,并对Transformer Decoder进行了一些改动,原本的Decoder包含了两个Multi-Head Attention结构,而GPT2只保留了Mask Multi-Head Attention。

liucong's avatar
liucong committed
9
10
## Python版本推理

liucong's avatar
liucong committed
11
本次采用GPT-2模型进行诗词生成任务,模型文件下载链接:https://pan.baidu.com/s/1KWeoUuakCZ5dualK69qCcw , 提取码:4pmh ,并将GPT2_shici.onnx模型文件保存在Resource/文件夹下。下面介绍如何运行python代码示例,具体推理代码解析,在Doc/Tutorial_Python.md目录中有详细说明。
liucong's avatar
liucong committed
12
13

### 安装镜像
14
15
16
17

在光源可拉取推理的docker镜像,GPT2模型推理的镜像如下: 

```python
18
docker pull image.sourcefind.cn:5000/dcu/admin/base/custom:ort1.14.0_migraphx3.0.0-dtk22.10.1
19
20
```

liucong's avatar
liucong committed
21
22
### 执行推理

liucong's avatar
liucong committed
23
1.参考《MIGraphX教程》设置好PYTHONPATH
liucong's avatar
liucong committed
24
25
26
27

2.安装依赖:

```python
liucong's avatar
liucong committed
28
29
# 进入gpt2 migraphx工程根目录
cd <path_to_gpt2_migraphx> 
liucong's avatar
liucong committed
30
31
32
33
34
35
36
37
38

# 进入示例程序目录
cd ./Python/

# 安装依赖
pip install -r requirements.txt
```

3.设置环境变量:
39
40

```python
liucong's avatar
liucong committed
41
42
43
44
# 设置动态shape模式
export MIGRAPHX_DYNAMIC_SHAPE=1
```

liucong's avatar
liucong committed
45
4.在Python目录下执行如下命令运行该示例程序:
liucong's avatar
liucong committed
46
47
48

```python
python gpt2.py
49
50
```

liucong's avatar
liucong committed
51
52
53
54
55
56
57
58
59
60
如下所示,采用交互式界面,通过输入开头诗词,GPT2模型可以生成后续的诗句。

<img src="./Doc/Images/GPT_03.png" style="zoom:80%;" align=middle>

## C++版本推理

下面介绍如何运行python代码示例,具体推理代码解析,在Doc/Tutorial_Cpp目录中有详细说明。

参考Python版本推理中的构建安装,在光源中拉取推理的docker镜像。

61
62
63
64
65
66
67
68
### 修改CMakeLists.txt

- 如果使用ubuntu系统,需要修改CMakeLists.txt中依赖库路径:
  将"${CMAKE_CURRENT_SOURCE_DIR}/depend/lib64/"修改为"${CMAKE_CURRENT_SOURCE_DIR}/depend/lib/"

- **MIGraphX2.3.0及以上版本需要c++17**


liucong's avatar
liucong committed
69
### 构建工程
70
71
72
73
74
75
76
77
78
79
80
81

```
rbuild build -d depend
```

### 设置环境变量

将依赖库依赖加入环境变量LD_LIBRARY_PATH,在~/.bashrc中添加如下语句:

**Centos**:

```
liucong's avatar
liucong committed
82
export LD_LIBRARY_PATH=<path_to_gpt2_migraphx>/depend/lib64/:$LD_LIBRARY_PATH
83
84
85
86
87
```

**Ubuntu**:

```
liucong's avatar
liucong committed
88
export LD_LIBRARY_PATH=<path_to_gpt2_migraphx>/depend/lib/:$LD_LIBRARY_PATH
89
90
91
92
93
94
```

然后执行:

```
source ~/.bashrc
95
96
```

liucong's avatar
liucong committed
97
### 执行推理
98

liucong's avatar
liucong committed
99
运行GPT2示例程序,具体执行如下命令:
100
101

```python
liucong's avatar
liucong committed
102
103
# 进入gpt2 migraphx工程根目录
cd <path_to_gpt2_migraphx> 
104

liucong's avatar
liucong committed
105
# 进入build目录
106
107
108
109
110
cd ./build/

# 设置动态shape模式
export MIGRAPHX_DYNAMIC_SHAPE=1

liucong's avatar
liucong committed
111
112
# 执行示例程序
./GPT2
113
114
115
```

如下所示,采用交互式界面,通过输入开头诗词,GPT2模型可以推理出后续的诗句。
116

liucong's avatar
liucong committed
117
<img src="./Doc/Images/GPT_04.png" style="zoom:100%;" align=middle>
118
119
120
121
122
123
124
125
126
127

## 历史版本

https://developer.hpccube.com/codes/modelzoo/gpt2_migraphx

## 参考资料

https://github.com/yangjianxin1/GPT2-chitchat

https://github.com/Morizeyao/GPT2-Chinese