-
Notifications
You must be signed in to change notification settings - Fork 173
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
【Hackathon 6th No.58】support model convert from fp32 to fp16 #1268
【Hackathon 6th No.58】support model convert from fp32 to fp16 #1268
Conversation
@Zheng-Bicheng 大佬,具体做的时候有以下几个问题:
|
Paddle2ONNX 目前支持 FP16 模型的方式类似把 FP32 格式的 Paddle 模型转换为 FP32 的ONNX模型,然后再把 FP32 的 ONNX 转为 FP16 的 ONNX 模型。这次的任务是直接转换 FP16 格式的 Paddle 模型。
基本上可以按照以下步骤进行:
|
FP16的Resnet模型可以通过类似以下方式来实现
|
这里我看了一下PaddleClas相关的代码,Resnet看上去好像没有from_pretrained这种调用方式,所以我直接通过下载模型及其权重来转换了。 |
针对第二点,请问一下具体是什么呢?比如batch_norm算子是对相关float的参数做处理? 另外用paddle原生直接对resnet的模型做半精度推理,是不支持的吗,报了下面的错:
|
没问题的 |
要用GPU推理我记得,这个最好是去Paddle的Issues提一下 |
我这里已经用的是gpu进行推理,我去issue上问一下 |
比如 layer_norm.cc#L35 这里会强转FP16为FP32,需要针对性添加支持 |
resnet中没有layernorm,所以我对matmul修改了一下,麻烦看一下这样修改是否正确,没问题的话我剩下相关的layer都这样处理。 另外像batchnorm这些层该怎么升级op处理呢?这里说实话有点看不懂,这里有相关的dtype constraints,第一行看上去float,float16这些都支持,后面又说限制float,这里具体怎么处理呢? |
我只是举了个例子,可以按实际情况来处理,例如如果实际没有这个算子就不需要升级 |
@xiaoyewww 这个有些问题,以你这里适配的matmul为例子:
|
@Zheng-Bicheng 大佬,我修改layer_norm这个算子的时候遇到点问题,onnx上不是最低只支持opset17吗,怎么p2o上有opset7的支持呢? |
另外,这个误差范围多少合适呢,我目前比较的是fp32下的推理结果:
|
抱歉才看到,这里我看不是支持的吗,opset13支持的是BF16? |
这里看错了,确实是支持FP16的,没有问题 |
Opset7的支持是把这个算子拆分成很多个其他算子来做的 |
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.
以下CI出现了问题,麻烦再看看
2024-06-12T16:03:46.7357707Z ============ failed cases =============
2024-06-12T16:03:46.7358341Z ./test_argmax.py
2024-06-12T16:03:46.7358634Z ./test_argmin.py
2024-06-12T16:03:46.7358929Z ./test_argsort.py
2024-06-12T16:03:46.7359418Z ./test_auto_scan_argminmax.py
2024-06-12T16:03:46.7359794Z ./test_auto_scan_argsort.py
2024-06-12T16:03:46.7360129Z ./test_auto_scan_assign.py
2024-06-12T16:03:46.7360453Z ./test_auto_scan_avgpool.py
2024-06-12T16:03:46.7360806Z ./test_auto_scan_cast.py
2024-06-12T16:03:46.7361131Z ./test_auto_scan_eye.py
2024-06-12T16:03:46.7361456Z ./test_auto_scan_fill_constant.py
2024-06-12T16:03:46.7361822Z ./test_auto_scan_fill_like.py
2024-06-12T16:03:46.7362384Z ./test_auto_scan_gaussian_random.py
2024-06-12T16:03:46.7362770Z ./test_auto_scan_linspace.py
2024-06-12T16:03:46.7363501Z ./test_auto_scan_logical_ops.py
2024-06-12T16:03:46.7364071Z ./test_auto_scan_lookup_table_v2.py
2024-06-12T16:03:46.7364638Z ./test_auto_scan_matmul.py
2024-06-12T16:03:46.7365140Z ./test_auto_scan_one_hot_v2.py
2024-06-12T16:03:46.7365527Z ./test_auto_scan_pool_adaptive_avg_ops.py
2024-06-12T16:03:46.7365935Z ./test_auto_scan_pool_avg_ops.py
2024-06-12T16:03:46.7366295Z ./test_auto_scan_pool_max_ops.py
2024-06-12T16:03:46.7366772Z ./test_auto_scan_range.py
2024-06-12T16:03:46.7367271Z ./test_auto_scan_reduce_all_or_any.py
2024-06-12T16:03:46.7367879Z ./test_auto_scan_shape.py
2024-06-12T16:03:46.7368270Z ./test_auto_scan_size.py
2024-06-12T16:03:46.7368589Z ./test_auto_scan_where_index.py
2024-06-12T16:03:46.7368927Z ./test_cast.py
2024-06-12T16:03:46.7369185Z ./test_equal.py
2024-06-12T16:03:46.7369448Z ./test_greater_equal.py
2024-06-12T16:03:46.7369753Z ./test_greater_than.py
2024-06-12T16:03:46.7370051Z ./test_less_equal.py
2024-06-12T16:03:46.7370330Z ./test_less_than.py
2024-06-12T16:03:46.7370617Z ./test_nn_Embedding.py
2024-06-12T16:03:46.7370932Z ./test_nn_Functional_LogSoftmax.py
2024-06-12T16:03:46.7371283Z ./test_nonzero.py
2024-06-12T16:03:46.7371546Z ./test_numel.py
2024-06-12T16:03:46.7371811Z ./test_resnet_fp16.py
2024-06-12T16:03:46.7372087Z ./test_shape.py
2024-06-12T16:03:46.7372344Z total bugs: 37
2024-06-12T16:03:46.7385269Z ##[error]Process completed with exit code 37.
@Zheng-Bicheng
|
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.
以下CI仍然存在问题
============ failed cases =============
./test_auto_scan_assign.py
./test_auto_scan_matmul.py
./test_resnet_fp16.py
关于onnx的问题,您能帮忙在 README 中也做对应的修改吗? |
已修改 |
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.
这里再看下
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.
大佬,数据类型列表这块是不是改成储存支持的数据类型好一点?我看你这漏了不少数据类型啊。
paddle2onnx/mapper/tensor/matmul.h
Outdated
@@ -34,6 +34,7 @@ class MatmulMapper : public Mapper { | |||
|
|||
private: | |||
std::string GetTrans(std::vector<TensorInfo>& input_info); | |||
const std::unordered_set<int32_t> kNeedCastTypes{P2ODataType::INT8, P2ODataType::FP64}; |
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.
这里int16也是不支持的
@@ -33,6 +33,7 @@ class MatmulV2Mapper : public Mapper { | |||
|
|||
private: | |||
std::string GetTrans(std::vector<TensorInfo>& input_info); | |||
const std::unordered_set<int32_t> kNeedCastTypes{P2ODataType::INT8, P2ODataType::FP64}; |
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.
这里int16也是不支持的
郑师傅,我这里这样理解的:
比如对pool这个算子,因为没有int8的测例,但又因为这是onnx支持的,所以我第一反应是这个算子是不需要cast的,所以没放在列表里 |
@Zheng-Bicheng 已按要求修改,麻烦再review一下 |
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.
LGTM
Details:
support model convert from fp32 to fp16