Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
32d5c55c
X
X2Paddle
项目概览
PaddlePaddle
/
X2Paddle
大约 1 年 前同步成功
通知
328
Star
698
Fork
167
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
26
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
X
X2Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
26
Issue
26
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
32d5c55c
编写于
6月 30, 2022
作者:
W
wjj19950828
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add doc for HuggingFace
上级
3161300b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
105 addition
and
0 deletion
+105
-0
docs/inference_model_convertor/toolkits/HuggingFace2paddle.md
.../inference_model_convertor/toolkits/HuggingFace2paddle.md
+105
-0
未找到文件。
docs/inference_model_convertor/toolkits/HuggingFace2paddle.md
0 → 100644
浏览文件 @
32d5c55c
# HuggingFace 模型导出为 Paddle 模型教程
X2Paddle 新增对 HuggingFace 模型支持,目前模型支持的列表如下
## TorchScript
<font
size=
0.5
>
| |
<font
size=
2
>
CausalLM |
<font
size=
2
>
MaskedLM |
<font
size=
2
>
Seq2SeqLM |
<font
size=
2
>
SequenceClassification |
<font
size=
2
>
MultipleChoice |
<font
size=
2
>
NextSentencePrediction |
<font
size=
2
>
TokenClassification |
<font
size=
2
>
QuestionAnswering |
<font
size=
2
>
AudioClassification |
|---|---|---|---|---|---|---|---|---|---|
|
<font
size=
2
>
[
BERT
](
https://huggingface.co/docs/transformers/main/model_doc/bert#transformers.BertModel
)
|✅ | ✅ | None |✅|✅|✅|✅|✅|None|
|
<font
size=
2
>
[
RoBERTa
](
https://huggingface.co/docs/transformers/main/model_doc/roberta#transformers.RobertaModel
)
|✅ |✅ | None |✅|✅| None |✅|✅| None |
|
<font
size=
2
>
[
T5
](
https://huggingface.co/docs/transformers/main/model_doc/t5#transformers.T5Model
)
| None | None |✅|None|None|None|None|None|None|
|
<font
size=
2
>
[
GPT2
](
https://huggingface.co/docs/transformers/main/model_doc/gpt2#transformers.GPT2Model
)
|✅ | None |None|✅|None|None|✅|None|None|
|
<font
size=
2
>
[
MarianMT
](
https://huggingface.co/docs/transformers/main/model_doc/marian#transformers.MarianModel
)
|✅ | None |✅|None|None|None|None|None|None|
|
<font
size=
2
>
[
ELECTRA
](
https://huggingface.co/docs/transformers/main/model_doc/electra#transformers.ElectraModel
)
| None | ✅ |None|✅|✅|None|✅|✅|None|
|
<font
size=
2
>
[
DistilBERT
](
https://huggingface.co/docs/transformers/main/model_doc/distilbert#transformers.DistilBertModel
)
|None | ✅ |None|✅|✅|None|✅|✅|None|
|
<font
size=
2
>
[
BART
](
https://huggingface.co/docs/transformers/main/model_doc/bart#transformers.BartModel
)
|✅ |✅ |✅|✅|None|None|None|✅|None|
|
<font
size=
2
>
[
XLM-RoBERTa
](
https://huggingface.co/docs/transformers/main/model_doc/xlm-roberta#transformers.XLMRobertaModel
)
|✅ |✅ | None |✅|✅|None|✅|✅|None|
|
<font
size=
2
>
[
ALBERT
](
https://huggingface.co/docs/transformers/main/model_doc/albert#transformers.AlbertModel
)
|None | ✅ |None|✅|✅|None|✅|✅|None|
|
<font
size=
2
>
[
LayoutLM
](
https://huggingface.co/docs/transformers/main/model_doc/layoutlm#transformers.LayoutLMModel
)
|None | ✅ |None|✅|None|None|✅|None|None|
|
<font
size=
2
>
[
BigBird
](
https://huggingface.co/docs/transformers/main/model_doc/big_bird#transformers.BigBirdModel
)
|✅ | ✅ |None|✅|✅|None|✅|✅|None|
|
<font
size=
2
>
[
Wav2Vec2
](
https://huggingface.co/docs/transformers/main/model_doc/wav2vec2#transformers.Wav2Vec2Model
)
| None | None |None|None|None|None|None|None|✅|
Notes:
-
上表的列表示 backbone 类型,行表示任务类型
-
None 表示该 backbone 不支持相关任务(与模型转换无关)
## ONNX
<font
size=
0.5
>
| |
<font
size=
2
>
CausalLM |
<font
size=
2
>
MaskedLM |
<font
size=
2
>
Seq2SeqLM |
<font
size=
2
>
SequenceClassification |
<font
size=
2
>
MultipleChoice |
<font
size=
2
>
NextSentencePrediction |
<font
size=
2
>
TokenClassification |
<font
size=
2
>
QuestionAnswering |
<font
size=
2
>
AudioClassification |
|---|---|---|---|---|---|---|---|---|---|
|
<font
size=
2
>
[
BERT
](
https://huggingface.co/docs/transformers/main/model_doc/bert#transformers.BertModel
)
|✅ | ✅ | None |✅|None|None|✅|✅|None|
|
<font
size=
2
>
[
RoBERTa
](
https://huggingface.co/docs/transformers/main/model_doc/roberta#transformers.RobertaModel
)
|✅ |✅ | None |✅|None| None |✅|✅| None |
|
<font
size=
2
>
[
T5
](
https://huggingface.co/docs/transformers/main/model_doc/t5#transformers.T5Model
)
| None | None |✅|None|None|None|None|None|None|
|
<font
size=
2
>
[
GPT2
](
https://huggingface.co/docs/transformers/main/model_doc/gpt2#transformers.GPT2Model
)
|✅ | None |None|✅|None|None|✅|None|None|
|
<font
size=
2
>
[
MarianMT
](
https://huggingface.co/docs/transformers/main/model_doc/marian#transformers.MarianModel
)
|✅ | None |✅|None|None|None|None|None|None|
|
<font
size=
2
>
[
ELECTRA
](
https://huggingface.co/docs/transformers/main/model_doc/electra#transformers.ElectraModel
)
| None | ✅ |None|✅|None|None|✅|✅|None|
|
<font
size=
2
>
[
DistilBERT
](
https://huggingface.co/docs/transformers/main/model_doc/distilbert#transformers.DistilBertModel
)
|None | ✅ |None|✅|None|None|✅|✅|None|
|
<font
size=
2
>
[
BART
](
https://huggingface.co/docs/transformers/main/model_doc/bart#transformers.BartModel
)
|✅ |None |✅|✅|None|None|None|✅|None|
|
<font
size=
2
>
[
XLM-RoBERTa
](
https://huggingface.co/docs/transformers/main/model_doc/xlm-roberta#transformers.XLMRobertaModel
)
|✅ |✅ | None |✅|None|None|✅|✅|None|
|
<font
size=
2
>
[
ALBERT
](
https://huggingface.co/docs/transformers/main/model_doc/albert#transformers.AlbertModel
)
|None | ✅ |None|✅|None|None|✅|✅|None|
|
<font
size=
2
>
[
LayoutLM
](
https://huggingface.co/docs/transformers/main/model_doc/layoutlm#transformers.LayoutLMModel
)
|None | ✅ |None|✅|None|None|✅|None|None|
Notes:
-
上表的列表示 backbone 类型,行表示任务类型
-
None 表示 HuggingFace 不支持 ONNX 模型导出(与模型转换无关)
## 转换教程
### 环境依赖
-
python >= 3.5
-
Huggingface 4.16.0
-
PyTorch 1.7.1
-
PaddlePaddle 2.3.0
-
ONNX 1.9.0
### Torch模型转换到Paddle
以模型为 Bert、任务类型为 SequenceClassification 为例,运行如下代码:
```
code
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from x2paddle.convert import pytorch2paddle
save_dir = "pd_model"
jit_type = "trace"
# Load tokenizer and PyTorch weights form the Hub
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased", return_dict=False)
inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
pt_model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased", return_dict=False)
pt_model.eval()
result = pt_model(inputs["input_ids"], inputs["attention_mask"], inputs["token_type_ids"])
# convert
pytorch2paddle(pt_model, save_dir , jit_type, [inputs["input_ids"], inputs["attention_mask"], inputs["token_type_ids"]])
```
### ONNX 模型转换到 Paddle
#### 步骤一、通过 HuggingFace 导出 ONNX 模型
使用命令行
```
shell
python
-m
transformers.onnx
--model
=
bert-base-uncased onnx/
```
更多细节可参考 HuggingFace
[
ONNX导出教程
](
https://huggingface.co/docs/transformers/main/serialization#exporting-a-model-to-onnx
)
#### 步骤二、通过X2Paddle将ONNX模型转换为Paddle格式
通过 X2Paddle 将 ONNX 模型转换为 Paddle
```
shell
x2paddle
--framework
=
onnx
--model
=
model.onnx
--save_dir
=
pd_model_dynamic
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录