-
Notifications
You must be signed in to change notification settings - Fork 661
Add uie cpp deploy #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add uie cpp deploy #120
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
94a35f3
implement PredictUIEInput
joey12300 5434758
remove batch size
joey12300 f5537a9
Add GetCandidateIdx
joey12300 2647cf3
add GetCandidateIdx GetSpan GetSpanIdxAndProbs
joey12300 48b3847
Add Predict of UIEModel
joey12300 c9c3b69
Add relation schema
joey12300 52e7dc5
Fix uie unicode bug
joey12300 a7a1cd4
rename information_extraction/ernie -> uie
joey12300 3f94c2a
Add more uie task
joey12300 cf8df59
Add cross task extraction
joey12300 134e7d3
use CharToBytesOffsetConverter
joey12300 b933b49
Add faster_tokenizer dir
joey12300 7263c24
Add RuntimeOption args
joey12300 95369f1
Add todo comments
joey12300 c25926e
Add some readme
joey12300 342c197
fix readme
joey12300 30c8784
Fix readme
joey12300 4b834de
Merge branch 'develop' into add_uie
jiangjiajun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 0 additions & 182 deletions
182
examples/text/information_extraction/ernie/cpp/infer.cc
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # 通用信息抽取 UIE C++部署示例 | ||
|
|
||
| 本目录下提供`infer.cc`快速完成[UIE模型](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/model_zoo/uie)在CPU/GPU的示例。 | ||
|
|
||
| 在部署前,需确认以下两个步骤 | ||
|
|
||
| - 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../docs/quick_start/requirements.md) | ||
| - 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../docs/compile/prebuilt_libraries.md) | ||
|
|
||
| 以Linux上uie-base模型推理为例,在本目录执行如下命令即可完成编译测试。 | ||
|
|
||
| ``` | ||
| # UIE目前还未发布,当前需开发者自行编译FastDeploy,通过如下脚本编译得到部署库fastdeploy-linux-x64-dev | ||
| git clone https://github.com/PaddlePaddle/FastDeploy.git | ||
| cd FastDeploy | ||
| mkdir build && cd build | ||
| cmake .. -DENABLE_ORT_BACKEND=ON \ | ||
| -DENABLE_VISION=ON \ | ||
| -DENABLE_PADDLE_BACKEND=ON \ | ||
| -DENABLE_TEXT=ON \ | ||
| -DWITH_GPU=ON \ | ||
| -DCMAKE_INSTALL_PREFIX=${PWD}/fastdeploy-linux-x64-gpu-dev | ||
|
|
||
| make -j8 | ||
| make install | ||
|
|
||
| # 编译模型examples代码(SDK中包含了examples代码) | ||
| cd ../examples/text/uie/cpp | ||
| mkdir build | ||
| cd build | ||
| cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/../../../../../build/fastdeploy-linux-x64-gpu-dev | ||
| make -j | ||
|
|
||
| # 下载uie-base模型以及词表 | ||
| wget https://bj.bcebos.com/fastdeploy/models/uie/uie-base.tgz | ||
| tar -xvfz uie-base.tgz | ||
|
|
||
|
|
||
| # CPU 推理 | ||
| ./infer_demo uie-base 0 | ||
|
|
||
| # GPU 推理 | ||
| ./infer_demo uie-base 1 | ||
| ``` | ||
|
|
||
| ## 模型获取 | ||
| UIE 模型介绍可以参考https://github.com/PaddlePaddle/PaddleNLP/tree/develop/model_zoo/uie 。其中,在完成训练后,需要将训练后的模型导出成推理模型。该步骤可参考该文档完成导出:https://github.com/PaddlePaddle/PaddleNLP/tree/develop/model_zoo/uie#%E6%A8%A1%E5%9E%8B%E9%83%A8%E7%BD%B2 。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| #include <iostream> | ||
| #include <sstream> | ||
|
|
||
| #include "fastdeploy/function/reduce.h" | ||
| #include "fastdeploy/function/softmax.h" | ||
| #include "fastdeploy/text.h" | ||
| #include "faster_tokenizer/tokenizers/ernie_faster_tokenizer.h" | ||
| #include "uie.h" | ||
|
|
||
| using namespace paddlenlp; | ||
|
|
||
| #ifdef WIN32 | ||
| const char sep = '\\'; | ||
| #else | ||
| const char sep = '/'; | ||
| #endif | ||
|
|
||
| int main(int argc, char* argv[]) { | ||
| if (argc < 3) { | ||
| std::cout << "Usage: infer_demo path/to/model run_option, " | ||
| "e.g ./infer_demo uie-base 0" | ||
| << std::endl; | ||
| std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " | ||
| "with gpu." | ||
| << std::endl; | ||
| return -1; | ||
| } | ||
| auto option = fastdeploy::RuntimeOption(); | ||
| if (std::atoi(argv[2]) == 0) { | ||
| option.UseCpu(); | ||
| } else { | ||
| option.UseGpu(); | ||
| } | ||
| std::string model_dir(argv[1]); | ||
| std::string model_path = model_dir + sep + "inference.pdmodel"; | ||
| std::string param_path = model_dir + sep + "inference.pdiparams"; | ||
| std::string vocab_path = model_dir + sep + "vocab.txt"; | ||
|
|
||
| auto predictor = UIEModel(model_path, param_path, vocab_path, 0.5, 128, | ||
| {"时间", "选手", "赛事名称"}, option); | ||
| fastdeploy::FDINFO << "After init predictor" << std::endl; | ||
| std::vector<std::unordered_map<std::string, std::vector<UIEResult>>> results; | ||
| // Named Entity Recognition | ||
| predictor.Predict({"2月8日上午北京冬奥会自由式滑雪女子大跳台决赛中中国选手谷" | ||
| "爱凌以188.25分获得金牌!"}, | ||
| &results); | ||
| std::cout << results << std::endl; | ||
| results.clear(); | ||
|
|
||
| // Relation Extraction | ||
| predictor.SetSchema({{"竞赛名称", | ||
| {SchemaNode("主办方"), SchemaNode("承办方"), | ||
| SchemaNode("已举办次数")}}}); | ||
| predictor.Predict( | ||
| {"2022语言与智能技术竞赛由中国中文信息学会和中国计算机学会联合主办,百度" | ||
| "公司、中国中文信息学会评测工作委员会和中国计算机学会自然语言处理专委会" | ||
| "承办,已连续举办4届,成为全球最热门的中文NLP赛事之一。"}, | ||
| &results); | ||
| std::cout << results << std::endl; | ||
| results.clear(); | ||
|
|
||
| // Event Extraction | ||
| predictor.SetSchema({{"地震触发词", | ||
| {SchemaNode("地震强度"), SchemaNode("时间"), | ||
| SchemaNode("震中位置"), SchemaNode("震源深度")}}}); | ||
| predictor.Predict( | ||
| {"中国地震台网正式测定:5月16日06时08分在云南临沧市凤庆县(北纬24." | ||
| "34度,东经99.98度)发生3.5级地震,震源深度10千米。"}, | ||
| &results); | ||
| std::cout << results << std::endl; | ||
| results.clear(); | ||
|
|
||
| // Opinion Extraction | ||
| predictor.SetSchema( | ||
| {{"评价维度", | ||
| {SchemaNode("观点词"), SchemaNode("情感倾向[正向,负向]")}}}); | ||
| predictor.Predict( | ||
| {"店面干净,很清静,服务员服务热情,性价比很高,发现收银台有排队"}, | ||
| &results); | ||
| std::cout << results << std::endl; | ||
| results.clear(); | ||
|
|
||
| // Sequence classification | ||
| predictor.SetSchema({"情感倾向[正向,负向]"}); | ||
| predictor.Predict({"这个产品用起来真的很流畅,我非常喜欢"}, &results); | ||
| std::cout << results << std::endl; | ||
| results.clear(); | ||
|
|
||
| // Cross task extraction | ||
|
|
||
| predictor.SetSchema({{"法院", {}}, | ||
| {"原告", {SchemaNode("委托代理人")}}, | ||
| {"被告", {SchemaNode("委托代理人")}}}); | ||
| predictor.Predict({"北京市海淀区人民法院\n民事判决书\n(199x)" | ||
| "建初字第xxx号\n原告:张三。\n委托代理人李四,北京市 " | ||
| "A律师事务所律师。\n被告:B公司,法定代表人王五,开发公司" | ||
| "总经理。\n委托代理人赵六,北京市 C律师事务所律师。"}, | ||
| &results); | ||
| std::cout << results << std::endl; | ||
| results.clear(); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里需要升级FasterTokenizer的目录结构。