【multi precision】multi precision support (fp32 + fp16)#9339
【multi precision】multi precision support (fp32 + fp16)#9339zhupengyang merged 13 commits intoPaddlePaddle:developfrom
Conversation
| if (LITE_ON_TINY_PUBLISH) | ||
| add_definitions("-DLITE_ON_TINY_PUBLISH") | ||
| add_definitions("-DLITE_ON_FLATBUFFERS_DESC_VIEW") | ||
| add_definitions("-DLITE_WITH_FLATBUFFERS_DESC") |
| const std::shared_ptr<const cpp::ProgramDesc>& program_desc) { | ||
| const std::shared_ptr<const cpp::ProgramDesc>& program_desc, | ||
| bool use_precision_low) { | ||
| auto* exe_scope = &scope_->NewScope(); |
| if (op_type != "feed" && op_type != "fetch") { | ||
| if (place.precision == PRECISION(kFloat)) { | ||
| place.precision = PRECISION(kFP16); | ||
| } else if (place.precision == PRECISION(kAny)) { |
There was a problem hiding this comment.
判断可以合并
if (place.precision == PRECISION(kFloat) || place.precision == PRECISION(kAny)) {
function()...
}
lite/core/op_lite.cc
Outdated
| op_type_, place.target, place.precision, place.layout); | ||
| VLOG(5) << "pick kernel for " << op_info()->Type() << " " | ||
| << place.DebugString() << " get " << ks.size() << " kernels"; | ||
| // VLOG(5) << "pick kernel for " << op_info()->Type() << " " |
lite/core/op_lite.cc
Outdated
|
|
||
| #ifdef LITE_ON_FLATBUFFERS_DESC_VIEW | ||
| bool OpLite::Attach(const cpp::OpDescWrite &opdesc, lite::Scope *scope) { | ||
| // valid_places_.clear(); |
lite/core/program.cc
Outdated
| int low_precision = 1; | ||
| std::string old_op; | ||
|
|
||
| if (use_precision_low == true) { |
There was a problem hiding this comment.
这里的逻辑直接改成这样更直观点?
use_precision_low_ = use_precision_low;
low_precision = use_precision_low_ ? 1 : 0;
lite/core/program.cc
Outdated
| #ifdef ENABLE_ARM_FP16 | ||
| if (lite::DeviceInfo::Global().has_fp16() && low_precision == 1) { | ||
| if (op_type != "feed" && op_type != "fetch") { | ||
| if (place.precision == static_cast<PrecisionType>(1)) { |
There was a problem hiding this comment.
直接用PrecisionType::kFloat这类枚举值,不要用1、5这类hard code,下同
| place.precision = static_cast<PrecisionType>(5); | ||
| } | ||
| } | ||
| // transfer weight to fp16 |
There was a problem hiding this comment.
下面的代码是不是跟WeightFP32ToFP16这个函数重复了?
| // kernels = op->CreateKernels({place}); | ||
| //} | ||
| if (kernels.size() == 0 && place.target == TargetType::kARM) { | ||
| place.target = TargetType::kHost; |
There was a problem hiding this comment.
这里是为什么要改成TargetType::kHost呢?
There was a problem hiding this comment.
因为有些fp16的kernal是host下的
lite/api/paddle_api.h
Outdated
| // whether to load data from memory. Model data will be loaded from memory | ||
| // buffer if model_from_memory_ is true. | ||
| bool model_from_memory_{false}; | ||
| PrecisionMode pre_mode_{LITE_PRECISION_NORMAL}; |
There was a problem hiding this comment.
不用简写成pre_mode吧,直接用 precision_mode_
|
|
||
| template <DataLayoutType DLType> | ||
| class CalibComputeFp32ToInt32 | ||
| : public KernelLite<TARGET(kARM), PRECISION(kInt32), DLType> { |
There was a problem hiding this comment.
PRECISION(kInt32)->PRECISION(kFloat)
|
|
||
| template <DataLayoutType DLType> | ||
| class CalibComputeFp32ToInt64 | ||
| : public KernelLite<TARGET(kARM), PRECISION(kInt64), DLType> { |
There was a problem hiding this comment.
PRECISION(kInt64)->PRECISION(kFloat)
lite/operators/calib_inplace_op.h
Outdated
| #ifdef LITE_ON_FLATBUFFERS_DESC_VIEW | ||
| bool AttachImpl(const cpp::OpDescWrite &opdesc, lite::Scope *scope) override; | ||
| #endif | ||
| void *getparam() { return ¶m_; } |
1.支持运行时混合精度

2.添加calib inplace算子
性能:
库体积影响:
+300KB