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
BERT_migraphx
Commits
2b2b9d16
Commit
2b2b9d16
authored
Sep 07, 2023
by
liucong
Browse files
更新示例工程
parent
86b38302
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
10 deletions
+56
-10
Doc/Tutorial_Cpp.md
Doc/Tutorial_Cpp.md
+12
-0
Doc/Tutorial_Python.md
Doc/Tutorial_Python.md
+1
-1
Python/bert.py
Python/bert.py
+17
-2
Src/Bert.cpp
Src/Bert.cpp
+26
-7
No files found.
Doc/Tutorial_Cpp.md
View file @
2b2b9d16
...
...
@@ -192,6 +192,18 @@ ErrorCode Bert::Inference(...)
2.
模型的推理结果是对输入序列中的每个词预测开始位置和结束位置的概率值,因此,分别采用start_position和end_position保存开始位置的概率值和结束位置的概率值,用于后续的数据后处理操作。
另外,如果想要指定输出节点,可以在eval()方法中通过提供outputNames参数来实现:
```
c++
...
// 推理
std
::
vector
<
std
::
string
>
outputNames
=
{
"unstack:0"
,
"unique_ids:0"
,
"unstack:1"
};
std
::
vector
<
migraphx
::
argument
>
results
=
net
.
eval
(
inputData
,
outputNames
);
...
```
如果没有指定outputName参数,则默认输出所有输出节点,此时输出节点的顺序与ONNX中输出节点顺序保持一致,可以通过netron查看ONNX文件的输出节点的顺序。
## 数据后处理
获得模型的推理结果后,并不能直接作为问题回答任务的结果显示,如下图所示,还需要进一步数据处理,得到最终的预测结果。
...
...
Doc/Tutorial_Python.md
View file @
2b2b9d16
...
...
@@ -134,7 +134,7 @@ def convert_examples_to_features(examples, tokenizer, max_seq_length,doc_stride,
2.
数据拼接,将获得的问题和上下文文本(子文本)拼接成一个序列,原理如下图所示:
<img
src=
".
.
/Images/Bert_02.png"
style=
"zoom:80%;"
align=
middle
>
<img
src=
"./Images/Bert_02.png"
style=
"zoom:80%;"
align=
middle
>
从图中可以看出,构建的方法是将问题和上下文文本拼接成一个序列,开头用[CLS]表示后面对应的问题,中间和最后用[SEP]符号隔开。其中,“[CLS]”是一个分类标志,表示后面的内容属于问题文本,“[SEP]”字符是一个分割标志,用来将问题和上下文文本分开。
...
...
Python/bert.py
View file @
2b2b9d16
...
...
@@ -29,9 +29,24 @@ tokenizer = tokenizers.BertWordPieceTokenizer(vocab_file)
# 使用run_onnx_squad中的convert_examples_to_features方法从输入中获取参数
input_ids
,
input_mask
,
segment_ids
,
extra_data
=
convert_examples_to_features
(
eval_examples
,
tokenizer
,
max_seq_length
,
doc_stride
,
max_query_length
)
# 设置最大输入shape
maxInput
=
{
"unique_ids_raw_output___9:0"
:[
1
],
"input_ids:0"
:[
1
,
256
],
"input_mask:0"
:[
1
,
256
],
"segment_ids:0"
:[
1
,
256
]}
# 加载模型
model
=
migraphx
.
parse_onnx
(
"../Resource/bertsquad-10.onnx"
,
map_input_dims
=
maxInput
)
# 获取模型输入/输出节点信息
print
(
"inputs:"
)
inputs
=
model
.
get_inputs
()
for
key
,
value
in
inputs
.
items
():
print
(
"{}:{}"
.
format
(
key
,
value
))
print
(
"outputs:"
)
outputs
=
model
.
get_outputs
()
for
key
,
value
in
outputs
.
items
():
print
(
"{}:{}"
.
format
(
key
,
value
))
# 编译
print
(
"INFO: Parsing and compiling the model..."
)
model
=
migraphx
.
parse_onnx
(
"../Resource/bertsquad-10.onnx"
)
model
.
compile
(
migraphx
.
get_target
(
"gpu"
),
device_id
=
0
)
n
=
len
(
input_ids
)
...
...
Src/Bert.cpp
View file @
2b2b9d16
...
...
@@ -28,28 +28,47 @@ ErrorCode Bert::Initialize()
// 获取模型文件
std
::
string
modelPath
=
"../Resource/bertsquad-10.onnx"
;
// 设置最大输入shape
migraphx
::
onnx_options
onnx_options
;
onnx_options
.
map_input_dims
[
"unique_ids_raw_output___9:0"
]
=
{
1
};
onnx_options
.
map_input_dims
[
"input_ids:0"
]
=
{
1
,
256
};
onnx_options
.
map_input_dims
[
"input_mask:0"
]
=
{
1
,
256
};
onnx_options
.
map_input_dims
[
"segment_ids:0"
]
=
{
1
,
256
};
// 加载模型
if
(
Exists
(
modelPath
)
==
false
)
{
LOG_ERROR
(
stdout
,
"%s not exist!
\n
"
,
modelPath
.
c_str
());
return
MODEL_NOT_EXIST
;
}
net
=
migraphx
::
parse_onnx
(
modelPath
);
net
=
migraphx
::
parse_onnx
(
modelPath
,
onnx_options
);
LOG_INFO
(
stdout
,
"succeed to load model: %s
\n
"
,
GetFileName
(
modelPath
).
c_str
());
// 获取模型输入属性
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
input
=
net
.
get_parameter_shapes
();
// 获取模型输入/输出节点信息
std
::
cout
<<
"inputs:"
<<
std
::
endl
;
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
inputs
=
net
.
get_inputs
();
for
(
auto
i
:
inputs
)
{
std
::
cout
<<
i
.
first
<<
":"
<<
i
.
second
<<
std
::
endl
;
}
std
::
cout
<<
"outputs:"
<<
std
::
endl
;
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
outputs
=
net
.
get_outputs
();
for
(
auto
i
:
outputs
)
{
std
::
cout
<<
i
.
first
<<
":"
<<
i
.
second
<<
std
::
endl
;
}
inputName1
=
"unique_ids_raw_output___9:0"
;
inputShape1
=
input
.
at
(
inputName1
);
inputShape1
=
input
s
.
at
(
inputName1
);
inputName2
=
"segment_ids:0"
;
inputShape2
=
input
.
at
(
inputName2
);
inputShape2
=
input
s
.
at
(
inputName2
);
inputName3
=
"input_mask:0"
;
inputShape3
=
input
.
at
(
inputName3
);
inputShape3
=
input
s
.
at
(
inputName3
);
inputName4
=
"input_ids:0"
;
inputShape4
=
input
.
at
(
inputName4
);
inputShape4
=
input
s
.
at
(
inputName4
);
// 设置模型为GPU模式
migraphx
::
target
gpuTarget
=
migraphx
::
gpu
::
target
{};
...
...
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