Skip to content

Commit

Permalink
MNN_HiAI IR op modify
Browse files Browse the repository at this point in the history
  • Loading branch information
946166920 committed Feb 19, 2024
1 parent 8bf486d commit c8fbd07
Show file tree
Hide file tree
Showing 60 changed files with 1,998 additions and 1,037 deletions.
327 changes: 109 additions & 218 deletions source/backend/hiai/backend/NPUBackend.cpp

Large diffs are not rendered by default.

59 changes: 45 additions & 14 deletions source/backend/hiai/backend/NPUBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <graph/compatible/all_ops.h>
#include <hiai_ir_build.h>
#include <graph/buffer.h>
#include <MNN/ErrorCode.hpp>
#include <core/Backend.hpp>
#include <core/Execution.hpp>
#include "HiAiModelManagerService.h"
Expand All @@ -42,6 +43,41 @@ namespace MNN {
typedef void *(*fp_ATrace_endSection) (void);
#endif
void NHWC2NCHW(const float* source, float* dest, int b, int c, int area);

static ge::DataType mapDataType(DataType src) {
ge::DataType retVal = ge::DataType::DT_UNDEFINED;
switch (src) {
case DataType_DT_FLOAT:
retVal = ge::DataType::DT_FLOAT;
break;
case DataType_DT_DOUBLE:
retVal = ge::DataType::DT_DOUBLE;
break;
case DataType_DT_INT32:
retVal = ge::DataType::DT_INT32;
break;
case DataType_DT_UINT8:
retVal = ge::DataType::DT_UINT8;
break;
case DataType_DT_INT16:
retVal = ge::DataType::DT_INT16;
break;
case DataType_DT_INT8:
retVal = ge::DataType::DT_INT8;
break;
case DataType_DT_INT64:
retVal = ge::DataType::DT_INT64;
break;
case DataType_DT_VARIANT:
retVal = ge::DataType::DT_FLOAT;
break;
default:
MNN_ASSERT(false);
printf("cast Datatype : %d \n", src);
break;
}
return retVal;
}
inline std::vector<int64_t> tensorShapeFormat(const Tensor *input, const Tensor *broadCastInput=nullptr) {
auto dimSize = input->buffer().dimensions;
if(broadCastInput != nullptr) {
Expand Down Expand Up @@ -267,24 +303,19 @@ namespace MNN {
virtual void onCopyBuffer(const Tensor* srcTensor, const Tensor* dstTensor) const override;

virtual void onResizeBegin() override;
virtual void onResizeEnd() override;
virtual ErrorCode onResizeEnd() override;

public:

void bulidIRModelAndLoad();

int process(int modelIndex) const ;
ErrorCode bulidIRModelAndLoad();
int process() const ;

shared_ptr<ge::Operator> getInputOps(const Op *op, int index = 0);

void setOutputOps(const Op *op, vector<shared_ptr<ge::Operator>>&& HIAI_op,
const std::vector<Tensor *> &outputs);
void setNetworkInput(const std::vector<Tensor *> &inputs, const Op* op);

private:

int getInOutTensorInfo(string modelName);

public:

map<int, vector<pair<shared_ptr<ge::Operator>, string>>> mGrapMap;
Expand All @@ -304,18 +335,18 @@ namespace MNN {
static bool addCreator(OpType t, Creator* c);

private:
shared_ptr<hiai::AiModelMngerClient> mMgrClient;

vector<string> mModelName;

vector<hiai::TensorDimension> mInputDimension;
vector<hiai::TensorDimension> mOutputDimension;

vector<shared_ptr<hiai::AiTensor>> mInputTensors;
vector<shared_ptr<hiai::AiTensor>> mOutputTensors;
MNNTensorList mMNNOutTensors;
const NPURuntime* mNPURuntime;
BackendConfig::PrecisionMode mPrecision;

shared_ptr<hiai::IBuiltModel> builtModel;
shared_ptr<hiai::IModelManager> modelManager;
vector<shared_ptr<hiai::INDTensorBuffer>> inputTensors;
vector<shared_ptr<hiai::INDTensorBuffer>> outputTensors;

#ifdef HIAI_DEBUG
void *(*ATrace_beginSection) (const char* sectionName);
void *(*ATrace_endSection) (void);
Expand Down
85 changes: 65 additions & 20 deletions source/backend/hiai/execution/NPUActivation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,84 @@ NPUActivation::NPUActivation(Backend *b, const Op *op, const std::vector<Tensor
ErrorCode NPUActivation::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
mNpuBackend->setNetworkInput(inputs, mOp);
auto opName = mOp->name()->str();



auto xOp = mNpuBackend->getInputOps(mOp);

if(mType == 5){
shared_ptr<hiai::op::PRelu> prelu(new hiai::op::PRelu(opName + "_prelu"));
auto slopePtr = mOp->main_as_PRelu()->slope()->data();
auto slopeSize = mOp->main_as_PRelu()->slope()->size();

mConst_w = hiai::op::Const(opName + "_w_const");
{
ge::TensorDesc fdesc(ge::Shape({1, slopeSize, 1, 1}), ge::FORMAT_NCHW,
ge::DT_FLOAT); // in o h w ?
auto inputIndex = mOp->inputIndexes()->data()[0];
auto iops = mNpuBackend->mGrapMap[inputIndex];
xOp = iops.back().first;
if (mType == OpType_PReLU && mOp->main_as_PRelu()->slope() != nullptr) {
if (mOp->main_as_PRelu()->slope()->size() == 1) {
const float* slopePtr = mOp->main_as_PRelu()->slope()->data();
shared_ptr<hiai::op::Activation> relu(new hiai::op::Activation(opName + "_relu"));
if (mNpuBackend->mSclipMap.find(inputIndex) == mNpuBackend->mSclipMap.end()) {
(*relu).set_input_x(*xOp.get());
} else {
(*relu).set_input_x(xOp->GetOutput(mNpuBackend->mSclipMap[inputIndex]));
}
(*relu)
.set_attr_coef(.000000)
.set_attr_negative_slope(*slopePtr)
.set_attr_mode(mType);
mNpuBackend->setOutputOps(mOp, {relu}, outputs);
} else {
shared_ptr<hiai::op::PRelu> prelu(new hiai::op::PRelu(opName + "_prelu"));
auto slopePtr = mOp->main_as_PRelu()->slope()->data();
auto slopeSize = mOp->main_as_PRelu()->slope()->size();
mConst_w = hiai::op::Const(opName + "_w_const");
ge::TensorDesc fdesc(ge::Shape({1, slopeSize, 1, 1}), ge::FORMAT_NCHW, ge::DT_FLOAT);
ge::TensorPtr filter = std::make_shared<ge::Tensor>();
filter->SetTensorDesc(fdesc);
filter->SetData((uint8_t *)slopePtr, slopeSize * sizeof(float));
mConst_w.set_attr_value(filter);
if (inputs[0]->buffer().dimensions < 4) {
std::vector<int32_t> shape;
for (int32_t i = 0; i < inputs[0]->buffer().dimensions; i++) {
shape.push_back(inputs[0]->buffer().dim[i].extent);
}
for (int32_t i = inputs[0]->buffer().dimensions; i < 4; i++) {
shape.push_back(1);
}
shapeConst = hiai::op::Const(opName +"_reshapeConst");
{
ge::TensorDesc fdesc(ge::Shape({static_cast<int64_t>(shape.size())}), ge::FORMAT_NCHW, ge::DT_INT32);
ge::TensorPtr filter = std::make_shared<ge::Tensor>();
filter->SetTensorDesc(fdesc);
filter->SetData((uint8_t *)shape.data(), shape.size() * sizeof(int32_t));
shapeConst.set_attr_value(filter);
}
shared_ptr<hiai::op::Reshape> reshape(new hiai::op::Reshape(opName + "_reshape"));
if (mNpuBackend->mSclipMap.find(inputIndex) == mNpuBackend->mSclipMap.end()) {
(*reshape).set_input_x(*xOp.get());
} else {
(*reshape).set_input_x(xOp->GetOutput(mNpuBackend->mSclipMap[inputIndex]));
}
(*reshape).set_input_shape(shapeConst);
(*prelu).set_input_x(*reshape.get()).set_input_weight(mConst_w);
mNpuBackend->setOutputOps(mOp, {reshape, prelu}, outputs);
} else {
if (mNpuBackend->mSclipMap.find(inputIndex) == mNpuBackend->mSclipMap.end()) {
(*prelu).set_input_x(*xOp.get());
} else {
(*prelu).set_input_x(xOp->GetOutput(mNpuBackend->mSclipMap[inputIndex]));
}
(*prelu).set_input_weight(mConst_w);
mNpuBackend->setOutputOps(mOp, {prelu}, outputs);
}
}

(*prelu)
.set_input_x(*xOp.get()).set_input_weight(mConst_w);
mNpuBackend->setOutputOps(mOp, {prelu}, outputs);
}else{
float slope = 0.0;
if (mOp->type() == OpType_ReLU) {
slope = mOp->main_as_Relu()->slope();
mType = 5;
if (slope != 0.0) {
mType = 5;
}
}

shared_ptr<hiai::op::Activation> relu(new hiai::op::Activation(opName + "_relu"));
if (mNpuBackend->mSclipMap.find(inputIndex) == mNpuBackend->mSclipMap.end()) {
(*relu).set_input_x(*xOp.get());
} else {
(*relu).set_input_x(xOp->GetOutput(mNpuBackend->mSclipMap[inputIndex]));
}
(*relu)
.set_input_x(*xOp.get())
.set_attr_coef(.000000)
.set_attr_negative_slope(slope)
.set_attr_mode(mType);
Expand Down
1 change: 1 addition & 0 deletions source/backend/hiai/execution/NPUActivation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class NPUActivation : public NPUCommonExecution {
virtual ~NPUActivation() = default;
private:
hiai::op::Const mConst_w;
hiai::op::Const shapeConst;
int mType;
};

Expand Down
2 changes: 1 addition & 1 deletion source/backend/hiai/execution/NPUArgMax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ErrorCode NPUArgMax::onResize(const std::vector<Tensor *> &inputs, const std::ve
// om input weight const op
mConst_axis = hiai::op::Const(opName + "_w_const");
{
auto aixs = axisFormat(inputs[0], argMaxParam->axis());
auto aixs = argMaxParam->axis();
ge::TensorDesc fdesc(ge::Shape({1}),ge::DT_INT32);
ge::TensorPtr axis = std::make_shared<ge::Tensor>();
axis->SetTensorDesc(fdesc);
Expand Down
93 changes: 93 additions & 0 deletions source/backend/hiai/execution/NPUBatchMatMul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// NPUBatchMatMul.cpp
// MNN
//
// Created by MNN on b'2020/10/15'.
// Copyright © 2018, Alibaba Group Holding Limited
//

#include "NPUBatchMatMul.hpp"
#include "NPUBackend.hpp"

using namespace std;

namespace MNN {

NPUBatchMatMul::NPUBatchMatMul(MNN::Backend *b, const MNN::Op *op, const std::vector<Tensor *> &inputs, const std::vector<MNN::Tensor *> &outputs) : NPUCommonExecution(b, op) {
auto opName = mOp->name()->str();

bool isConst0 = TensorUtils::getDescribe(inputs[0])->usage==Tensor::InsideDescribe::Usage::CONSTANT;
bool isConst1 = TensorUtils::getDescribe(inputs[1])->usage==Tensor::InsideDescribe::Usage::CONSTANT;

Tensor* input = nullptr;
if (isConst0 && !isConst1){
input = inputs[0];
}
if (!isConst0 && isConst1){
input = inputs[1];
}
if (input != nullptr) {
mConst = ge::op::Const(opName + "_w_const");
ge::TensorPtr filter = std::make_shared<ge::Tensor>();
vector<int64_t> dims;
for (int32_t i = 0; i < input->buffer().dimensions; i++) {
dims.push_back(input->buffer().dim[i].extent);
}
ge::TensorDesc fdesc(ge::Shape(dims), ge::FORMAT_NCHW, ge::DT_FLOAT);
if (input->getType().code == halide_type_int && input->getType().bits == 32) {
fdesc.SetDataType(ge::DT_INT32);
filter->SetData((uint8_t *)input->host<int32_t>(), input->elementSize() * sizeof(int32_t));
} else {
filter->SetData((uint8_t *)input->host<float>(), input->elementSize() * sizeof(float));
}
filter->SetTensorDesc(fdesc);
mConst.set_attr_value(filter);
}

}

ErrorCode NPUBatchMatMul::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
mNpuBackend->setNetworkInput(inputs, mOp);
auto opName = mOp->name()->str();
bool isConst0 = TensorUtils::getDescribe(inputs[0])->usage==Tensor::InsideDescribe::Usage::CONSTANT;
bool isConst1 = TensorUtils::getDescribe(inputs[1])->usage==Tensor::InsideDescribe::Usage::CONSTANT;
auto param = mOp->main_as_BatchMatMulParam();
shared_ptr<hiai::op::BatchMatMul> batchMatMul(new hiai::op::BatchMatMul(opName));
if (isConst0 && !isConst1) {
auto inputIndex1 = mOp->inputIndexes()->data()[1];
auto iops1 = mNpuBackend->mGrapMap[inputIndex1];
auto xOp1 = iops1.back().first;
(*batchMatMul)
.set_input_x1(mConst)
.set_input_x2(*xOp1.get())
.set_attr_adj_x1(param->adjX())
.set_attr_adj_x2(param->adjY());
} else if (!isConst0 && isConst1) {
auto inputIndex = mOp->inputIndexes()->data()[0];
auto iops = mNpuBackend->mGrapMap[inputIndex];
auto xOp = iops.back().first;
(*batchMatMul)
.set_input_x1(*xOp.get())
.set_input_x2(mConst)
.set_attr_adj_x1(param->adjX())
.set_attr_adj_x2(param->adjY());
} else {
auto inputIndex = mOp->inputIndexes()->data()[0];
auto iops = mNpuBackend->mGrapMap[inputIndex];
auto xOp = iops.back().first;
auto inputIndex1 = mOp->inputIndexes()->data()[1];
auto iops1 = mNpuBackend->mGrapMap[inputIndex1];
auto xOp1 = iops1.back().first;
(*batchMatMul)
.set_input_x1(*xOp.get())
.set_input_x2(*xOp1.get())
.set_attr_adj_x1(param->adjX())
.set_attr_adj_x2(param->adjY());
}
mNpuBackend->setOutputOps(mOp, {batchMatMul}, outputs);
return NO_ERROR;
}

NPUCreatorRegister<TypedCreator<NPUBatchMatMul>> __BatchMatMul_op(OpType_BatchMatMul);

} // namespace MNN
28 changes: 28 additions & 0 deletions source/backend/hiai/execution/NPUBatchMatMul.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// NPUBatchMatMul.hpp
// MNN
//
// Created by MNN on b'2020/10/15'.
// Copyright © 2018, Alibaba Group Holding Limited
//

#ifndef NPUDEMO_NPUBatchMatMul_HPP
#define NPUDEMO_NPUBatchMatMul_HPP

#include "NPUCommonExecution.hpp"
#include "NPUBackend.hpp"

namespace MNN {

class NPUBatchMatMul : public NPUCommonExecution {
public:
NPUBatchMatMul(Backend *b, const Op *op, const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs);
ErrorCode onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs);
virtual ~NPUBatchMatMul() = default;

private:
ge::op::Const mConst;
};
} // namespace MNN

#endif // NPUDEMO_NPUBatchMatMul_HPP
Loading

0 comments on commit c8fbd07

Please sign in to comment.