Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
GPT2_migraphx
Commits
3b905a5e
Commit
3b905a5e
authored
May 30, 2023
by
liucong
Browse files
修改部分文档
parent
7afec310
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
17 deletions
+6
-17
Doc/Tutorial_Cpp/GPT2.md
Doc/Tutorial_Cpp/GPT2.md
+3
-10
Doc/Tutorial_Python/GPT2.md
Doc/Tutorial_Python/GPT2.md
+3
-7
No files found.
Doc/Tutorial_Cpp/GPT2.md
View file @
3b905a5e
...
...
@@ -84,9 +84,7 @@ for(int i=0;i<50;++i)
在GPT2::Inference()函数具体实现了GPT-2模型的推理过程,主要做如下处理:
1.
reshape操作
2.
执行推理
1.
设置输入shape并执行推理
```
c++
long
unsigned
int
GPT2
::
Inference
(
const
std
::
vector
<
long
unsigned
int
>
&
input_id
)
...
...
@@ -99,12 +97,9 @@ long unsigned int GPT2::Inference(const std::vector<long unsigned int> &input_id
input
[
0
][
j
]
=
input_id
[
j
];
}
// 设置输入shape
并执行reshape
// 设置输入shape
std
::
vector
<
std
::
vector
<
std
::
size_t
>>
inputShapes
;
inputShapes
.
push_back
({
1
,
input_id
.
size
()});
std
::
unordered_map
<
std
::
string
,
std
::
vector
<
std
::
size_t
>>
inputShapeMap
;
inputShapeMap
[
inputName
]
=
inputShapes
[
0
];
migraphx
::
reshape2
(
net
,
inputShapeMap
);
// 输入数据
migraphx
::
parameter_map
inputData
;
...
...
@@ -123,9 +118,7 @@ long unsigned int GPT2::Inference(const std::vector<long unsigned int> &input_id
}
```
1.
reshape操作,因为GPT-2属于生成式语言模型,输入的shape一直在变化,所以在模型推理前需要reshape的操作。shape的大小根据输入数据的shape决定,之后执行reshape操作,从而使模型可以根据不同的shape进行推理。
2.
执行推理,GPT-2模型的推理结果results是一个std::vector
<
migraphx::argument
>
类型,包含一个输出,所以result = results[0]。result中一共包含了input_id.size()
*
22557个概率值,其中,input_id.size()代表输入数据的长度,22557代表了词汇表中词的数量。
1.
执行推理,GPT-2模型的推理结果results是一个std::vector
<
migraphx::argument
>
类型,包含一个输出,所以result = results[0]。result中一共包含了input_id.size()
*
22557个概率值,其中,input_id.size()代表输入数据的长度,22557代表了词汇表中词的数量。
## 数据后处理
...
...
Doc/Tutorial_Python/GPT2.md
View file @
3b905a5e
...
...
@@ -51,20 +51,16 @@ input_ids = np.expand_dims(input_ids, axis=0)
完成数据预处理后,就可以执行模型推理。推理过程主要做如下处理:
1.
re
shape
操作
1.
设置最大输入
shape
2.
循环推理
```
Python
# 设置最大输入shape
maxInput={"input":[1,10
24
]}
maxInput={"input":[1,10
00
]}
# 模型推理
for _ in range(max_len):
# 执行reshape
inputShapes = [input_ids.shape[0], input_ids.shape[1]]
inputShapeMap={inputName:inputShapes}
migraphx.reshape2(model, inputShapeMap)
# 推理
result = model.run({inputName: migraphx.argument(input_ids)})
...
...
@@ -83,7 +79,7 @@ for _ in range(max_len):
```
1.
re
shape
操作
,因为GPT-2属于生成式模型,输入的shape一直在变化,所以在模型推理前
需要进行reshape操作。首先,
设置一个最大的输入shape,maxInput={"input":[1,10
24
]},限定模型输入shape的范围。
其次,根据每次输入shape的具体大小,执行migraphx.reshape2()操作,从而使模型可以根据不同的shape进行推理。
1.
设置最大输入
shape,因为GPT-2属于生成式模型,输入的shape一直在变化,所以在模型推理前设置一个最大的输入shape,maxInput={"input":[1,10
00
]},限定模型输入shape的范围。
2.
循环推理,GPT-2模型不像其他模型一样只需要执行一次推理,而是需要循环执行多次推理才能完成。首先,模型推理限定在for循环中,将输入数据input_ids,输入到model.run({...})中执行推理,生成一个token的id。其次,将推理结果拼接到输入数据input_ids中,执行下一次循环。最后,当循环结束或者生成的词为[SEP]结束标志符时,完成GPT-2模型的整体推理。如下图所示,为GPT-2模型的一次完整推理过程。
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment