Skip to content

Commit

Permalink
release 0.2.0.6
Browse files Browse the repository at this point in the history
- fix bugs in quantization
- add evaluating tool for quantization
- add ADMM support in quantization
- fix lock in thread pool
- fix fusing for deconv
- fix reshape converting from ONNX to MNN
- turn off blob size checking by default
  • Loading branch information
liqing committed Aug 7, 2019
1 parent 444df58 commit f085106
Show file tree
Hide file tree
Showing 27 changed files with 1,073 additions and 303 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ option(MNN_USE_THREAD_POOL "Use Multiple Thread by Self ThreadPool" ON)
option(MNN_SUPPORT_TRAIN "Enable Train Ops" OFF)
option(MNN_BUILD_DEMO "Build demo/exec or not" OFF)
option(MNN_BUILD_QUANTOOLS "Build Quantized Tools or not" OFF)
option(MNN_EVALUATION "Build Evaluation Tools or not" OFF)

if (MNN_USE_THREAD_POOL)
set(MNN_OPENMP OFF)
Expand Down Expand Up @@ -409,6 +410,10 @@ if(MNN_BUILD_QUANTOOLS)
add_subdirectory(tools/quantization)
endif()

if(MNN_EVALUATION)
add_subdirectory(tools/evaluation)
endif()

# schema generator
if(WIN32)
add_custom_target( MNN_SCHEMA ALL
Expand Down
3 changes: 2 additions & 1 deletion source/backend/cpu/CPUConvInt8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Concurrency.h"
#include "Macro.h"
#include "TensorUtils.hpp"
#include <math.h>

#define UNIT 4
#define SRC_UNIT 16
Expand All @@ -35,7 +36,7 @@ inline int8_t int32ToInt8(int data, int bias, float scale) {
float value = (float)(data + bias) * scale;
value = std::max(value, -127.0f);
value = std::min(value, 127.0f);
return static_cast<int8_t>(value);
return static_cast<int8_t>(roundf(value));
}

static void MNNGemmInt8AddBiasScale_16x4_Unit(int8_t* dst, const int8_t* src, const int8_t* weight, const int32_t* bias,
Expand Down
3 changes: 2 additions & 1 deletion source/backend/cpu/CPUDepthwiseConvInt8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "CommonOptFunction.h"
#include "Concurrency.h"
#include "Macro.h"
#include <math.h>

#define UNIT 4

Expand All @@ -30,7 +31,7 @@ inline int8_t int32ToInt8(int data, int bias, float scale) {
float value = (float)(data + bias) * scale;
value = std::max(value, -127.0f);
value = std::min(value, 127.0f);
return static_cast<int8_t>(value);
return static_cast<int8_t>(roundf(value));
}

static void MNNDepthWiseInt8AddBiasScaleUnit(int8_t* dst, const int8_t* src, const int8_t* weight, const int32_t* bias,
Expand Down
1 change: 1 addition & 0 deletions source/backend/cpu/ThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ void ThreadPool::active() {
return;
}
gInstance->mActiveCount++;
std::lock_guard<std::mutex> _l(gInstance->mQueueMutex);
gInstance->mCondition.notify_all();
}
void ThreadPool::deactive() {
Expand Down
2 changes: 1 addition & 1 deletion tools/converter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ configure_file(

# -----------debug or no-----------
set(CMAKE_BUILD_TYPE debug)
option(MNN_CONVERT_DEBUG "Enable MNN CONVERT DEBUG" ON)
option(MNN_CONVERT_DEBUG "Enable MNN CONVERT DEBUG" OFF)
if(MNN_CONVERT_DEBUG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
Expand Down
3 changes: 2 additions & 1 deletion tools/converter/source/caffe/BatchNormalScale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class CuDNNBatchNorm : public OpConverter {
dstOp->main.value = bn;
auto& l = parameters;
auto w0 = &weight;
DCHECK(w0->blobs_size() >= 2) << "caffemodel error!";
const caffe::BlobProto& mean_blob = w0->blobs(0);
const caffe::BlobProto& var_blob = w0->blobs(1);
const caffe::BatchNormParameter& batch_norm_param = l.batch_norm_param();
Expand Down Expand Up @@ -137,7 +138,7 @@ class ScaleNode : public OpConverter {
auto w = &weight;
auto& l = parameters;
const caffe::LayerParameter* w0 = (const caffe::LayerParameter*)w;

DCHECK(w0->blobs_size() >= 1) << "caffemodel error!";
const caffe::BlobProto& weight_blob = w0->blobs(0);
const caffe::ScaleParameter& scale_param = l.scale_param();
sc->scaleData.resize(weight_blob.data_size());
Expand Down
1 change: 1 addition & 0 deletions tools/converter/source/caffe/InnerProduct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class InnerProduct : public InnerProductCommon {
auto innerproduct = dstOp->main.AsInnerProduct();
const caffe::InnerProductParameter& par = parameters.inner_product_param();
const caffe::LayerParameter* v0w = &weight;
DCHECK(v0w->blobs_size() >= 1) << "caffemodel error!";
innerproduct->biasTerm = par.bias_term();
innerproduct->bias.resize(par.num_output());
::memset(innerproduct->bias.data(), 0, innerproduct->bias.size() * sizeof(float));
Expand Down
3 changes: 2 additions & 1 deletion tools/converter/source/caffe/Relu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#include "OpConverter.hpp"
#include "logkit.h"

class Relu : public OpConverter {
public:
Expand Down Expand Up @@ -63,7 +64,7 @@ class PRelu : public OpConverter {
virtual void run(MNN::OpT* dstOp, const caffe::LayerParameter& parameters, const caffe::LayerParameter& weight) {
auto relu = new MNN::PReluT;
auto v0w = &weight;

DCHECK(v0w->blobs_size() >= 1) << "caffemodel error!";
const caffe::BlobProto& slope_blob = v0w->blobs(0);
relu->slopeCount = slope_blob.data_size();
relu->slope.resize(relu->slopeCount);
Expand Down
57 changes: 31 additions & 26 deletions tools/converter/source/optimizer/PostTreatUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const std::set<MNN::OpType> PostTreatUtils::NC4HW4_OPs = {
const std::set<MNN::OpType> PostTreatUtils::COMPABILITY_OPs = {
MNN::OpType_ReLU, MNN::OpType_ReLU6, MNN::OpType_Concat, MNN::OpType_Slice, MNN::OpType_Permute,
MNN::OpType_Selu, MNN::OpType_ConvertTensor, MNN::OpType_Sigmoid, MNN::OpType_Cast,
MNN::OpType_Reshape, MNN::OpType_TanH, MNN::OpType_ArgMax, MNN::OpType_Padding};
MNN::OpType_Reshape, MNN::OpType_TanH, MNN::OpType_ArgMax, MNN::OpType_Padding};

const std::vector<MNN::OpType> PostTreatUtils::DELETE_Ops = {
MNN::OpType_Seq2Out,
Expand Down Expand Up @@ -199,10 +199,25 @@ bool PostTreatUtils::_merge2Convolution(const MNN::OpT* inplaceOp, MNN::OpT* con
}
} else {
int weightPartSize = conv2D->weight.size() / outputCount;
for (int i = 0; i < outputCount; ++i) {
float a = alpha[i];
for (int j = 0; j < weightPartSize; ++j) {
conv2D->weight[i * weightPartSize + j] *= a;
if (convolutionOp->type == OpType_Deconvolution) {
int inputCount =
conv2D->weight.size() / outputCount / conv2D->common->kernelX / conv2D->common->kernelY;
for (int i = 0; i < inputCount; ++i) {
auto dstPos = i * outputCount * conv2D->common->kernelY * conv2D->common->kernelX;
for (int j = 0; j < outputCount; ++j) {
auto dstPosJ = dstPos + j * conv2D->common->kernelY * conv2D->common->kernelX;
float a = alpha[j];
for (int k = 0; k < conv2D->common->kernelY * conv2D->common->kernelX; ++k) {
conv2D->weight[dstPosJ + k] *= a;
}
}
}
} else {
for (int i = 0; i < outputCount; ++i) {
float a = alpha[i];
for (int j = 0; j < weightPartSize; ++j) {
conv2D->weight[i * weightPartSize + j] *= a;
}
}
}
}
Expand Down Expand Up @@ -391,9 +406,9 @@ void PostTreatUtils::addConverterForTensorFlowModel() {
// some ops in caffe are setted to be nc4hw4 layout which are different from tensorflow ops(nhwc default)
// static std::set<MNN::OpType> CAFFE_NC4HW4_OPS = {MNN::OpType_BinaryOp};

auto tensorDefaultType = MNN::MNN_DATA_FORMAT_NHWC;
if (mNet->sourceType == MNN::NetSource_CAFFE) {
tensorDefaultType = MNN::MNN_DATA_FORMAT_NC4HW4;
auto originTensorType = MNN::MNN_DATA_FORMAT_NHWC;
if (mNet->sourceType == MNN::NetSource_CAFFE) { // caffe or onnx
originTensorType = MNN::MNN_DATA_FORMAT_NCHW;
}

// set the layout of every tensor
Expand All @@ -402,42 +417,32 @@ void PostTreatUtils::addConverterForTensorFlowModel() {
std::map<std::string, MNN::MNN_DATA_FORMAT> opType;
for (auto& iter : mNet->oplists) {
// set output tensor layout of this op according to context
auto type = tensorDefaultType;
auto type = originTensorType;
if (iter->type == MNN::OpType_ConvertTensor) {
type = iter->main.AsTensorConvertInfo()->dest;
} else if (PostTreatUtils::NC4HW4_OPs.find(iter->type) != PostTreatUtils::NC4HW4_OPs.end()) {
type = MNN::MNN_DATA_FORMAT_NC4HW4;
} else if (PostTreatUtils::COMPABILITY_OPs.find(iter->type) != PostTreatUtils::COMPABILITY_OPs.end()) {
int caffeNumber = 0;
int tensorFlowNamer = 0;
int nc4hw4TypeNumber = 0; // NC4HW4 number
int originTypeNumber = 0;
for (int i = 0; i < iter->inputIndexes.size(); ++i) {
auto index = iter->inputIndexes[i];
if (_OpNeedConvertContent(iter->type, i)) {
if (tensorType[index] == MNN::MNN_DATA_FORMAT_NC4HW4) {
caffeNumber++;
} else if (tensorType[index] == tensorDefaultType) {
tensorFlowNamer++;
nc4hw4TypeNumber++;
} else if (tensorType[index] == originTensorType) {
originTypeNumber++;
}
}
}
if (caffeNumber > tensorFlowNamer) {
if (nc4hw4TypeNumber > originTypeNumber) {
type = MNN::MNN_DATA_FORMAT_NC4HW4;
} else {
if (mNet->sourceType == MNN::NetSource_CAFFE) {
type = MNN::MNN_DATA_FORMAT_NCHW;
} else {
type = MNN::MNN_DATA_FORMAT_NHWC;
}
}
if (iter->type == MNN::OpType_Reshape) {
if (iter->main.AsReshape()->dims.size() != 4) {
type = tensorDefaultType;
type = originTensorType;
}
}
} else if (mNet->sourceType == MNN::NetSource_CAFFE) {
// if (CAFFE_NC4HW4_OPS.find(iter->type) == CAFFE_NC4HW4_OPS.end()) {
type = MNN::MNN_DATA_FORMAT_NCHW;
// }
}

for (auto index : iter->outputIndexes) {
Expand Down
Loading

0 comments on commit f085106

Please sign in to comment.