optimize infershape when running graph

pull/4296/head
chenjianping 5 years ago
parent 3783dd04c0
commit 9a78a4a715

@ -46,6 +46,7 @@ int Concat::InferShape(std::vector<tensor::Tensor *> inputs_, std::vector<tensor
auto input0_shape_without_axis = input0_shape;
input0_shape_without_axis.erase(input0_shape_without_axis.begin() + axis);
auto input0_data_type = inputs_.at(0)->data_type();
schema::Format input0_format = inputs_[0]->GetFormat();
int output_axis_dim = input0_shape.at(axis);
for (size_t i = 1; i < inputs_.size(); ++i) {
if (inputs_.at(i)->data_type() != input0_data_type) {
@ -53,6 +54,10 @@ int Concat::InferShape(std::vector<tensor::Tensor *> inputs_, std::vector<tensor
return RET_PARAM_INVALID;
}
if (inputs_.at(i)->GetFormat() != input0_format) {
MS_LOG(ERROR) << "All input format should be the same!";
return RET_PARAM_INVALID;
}
auto shape_tmp = inputs_.at(i)->shape();
if (shape_tmp.size() != input0_shape.size()) {
MS_LOG(ERROR) << "All inputs should have the same dim num!";

@ -44,7 +44,13 @@ int Stack::InferShape(std::vector<tensor::Tensor *> inputs, std::vector<tensor::
MS_LOG(ERROR) << "Invalid axis " << stack_prim->axis();
return RET_PARAM_INVALID;
}
schema::Format input0_format = input->GetFormat();
for (size_t i = 1; i < inputs.size(); ++i) {
if (inputs[i]->GetFormat() != input0_format) {
MS_LOG(ERROR) << "All inputs should have the same format!";
return RET_PARAM_INVALID;
}
auto input_shape_tmp = inputs[i]->shape();
if (input_shape_tmp.size() != input_shape.size()) {
MS_LOG(ERROR) << "All input shape size should be the same!";

@ -27,9 +27,7 @@ class BatchToSpaceBaseCPUKernel : public LiteKernel {
BatchToSpaceBaseCPUKernel(OpParameter *parameter, const std::vector<lite::tensor::Tensor *> &inputs,
const std::vector<lite::tensor::Tensor *> &outputs, const lite::Context *ctx,
const lite::Primitive *primitive)
: LiteKernel(parameter, inputs, outputs, ctx, primitive) {
opParameter->thread_num_ = ctx->thread_num_;
}
: LiteKernel(parameter, inputs, outputs, ctx, primitive) {}
virtual ~BatchToSpaceBaseCPUKernel() = default;

@ -29,11 +29,9 @@ using mindspore::lite::RET_OK;
using mindspore::schema::PrimitiveType_Concat;
namespace mindspore::kernel {
int ConcatBaseCPUKernel::Init() {
if (context_->infer_shape_interrupt_ && !context_->running_) {
SetNeedReInit();
return RET_OK;
}
int ConcatBaseCPUKernel::Init() { return RET_OK; }
int ConcatBaseCPUKernel::ReSize() {
axis_ = concat_param_->axis_ >= 0 ? concat_param_->axis_ : inputs_.front()->shape().size() + concat_param_->axis_;
return RET_OK;
}

@ -31,7 +31,6 @@ class ConcatBaseCPUKernel : public LiteKernel {
const std::vector<lite::tensor::Tensor *> &outputs, const Context *ctx,
const lite::Primitive *primitive)
: LiteKernel(parameter, inputs, outputs, ctx, primitive), ctx_(ctx), thread_count_(ctx->thread_num_) {
opParameter->thread_num_ = ctx->thread_num_;
concat_param_ = reinterpret_cast<ConcatParameter *>(opParameter);
}
@ -39,7 +38,7 @@ class ConcatBaseCPUKernel : public LiteKernel {
int Init() override;
int ReSize() override { return 0; }
int ReSize() override;
int Run() override { return 0; }

@ -29,9 +29,7 @@ class CropBaseCPUKernel : public LiteKernel {
CropBaseCPUKernel(OpParameter *parameter, const std::vector<lite::tensor::Tensor *> &inputs,
const std::vector<lite::tensor::Tensor *> &outputs, const Context *ctx,
const lite::Primitive *primitive)
: LiteKernel(parameter, inputs, outputs, ctx, primitive), thread_count_(ctx->thread_num_) {
opParameter->thread_num_ = ctx->thread_num_;
}
: LiteKernel(parameter, inputs, outputs, ctx, primitive), thread_count_(ctx->thread_num_) {}
~CropBaseCPUKernel() = default;
int Init() override;

@ -27,9 +27,7 @@ class DepthToSpaceBaseCPUKernel : public LiteKernel {
DepthToSpaceBaseCPUKernel(OpParameter *parameter, const std::vector<lite::tensor::Tensor *> &inputs,
const std::vector<lite::tensor::Tensor *> &outputs, const lite::Context *ctx,
const lite::Primitive *primitive)
: LiteKernel(parameter, inputs, outputs, ctx, primitive) {
opParameter->thread_num_ = ctx->thread_num_;
}
: LiteKernel(parameter, inputs, outputs, ctx, primitive) {}
virtual ~DepthToSpaceBaseCPUKernel() = default;

@ -25,26 +25,18 @@ using mindspore::kernel::KERNEL_ARCH::kCPU;
using mindspore::lite::KernelRegistrar;
using mindspore::lite::RET_ERROR;
using mindspore::lite::RET_OK;
using mindspore::lite::RET_PARAM_INVALID;
using mindspore::schema::PrimitiveType_QuantDTypeCast;
namespace mindspore::kernel {
namespace {
constexpr int kQuantDTypeCastInputNum = 1;
constexpr int kQuantDTypeCastOutputNum = 1;
} // namespace
int QuantDTypeCastCPUKernel::Init() {
if (context_->infer_shape_interrupt_ && !context_->running_) {
SetNeedReInit();
return RET_OK;
}
if (inputs_.size() != 1) {
MS_LOG(ERROR) << "inputs number should be 1, but " << inputs_.size() << " is given.";
return RET_ERROR;
return RET_PARAM_INVALID;
}
if (outputs_.size() != 1) {
MS_LOG(ERROR) << "outputs number should be 1, but " << inputs_.size() << " is given.";
return RET_ERROR;
return RET_PARAM_INVALID;
}
auto in_tensor = inputs_.front();
auto out_tensor = outputs_.front();
@ -63,18 +55,23 @@ int QuantDTypeCastCPUKernel::Init() {
inverse_ = true;
} else {
MS_LOG(ERROR) << "param data type not supported:" << " src: " << param->srcT << " dst: " << param->dstT;
return RET_ERROR;
return RET_PARAM_INVALID;
}
if (!InferShapeDone()) {
return RET_OK;
}
return ReSize();
}
int QuantDTypeCastCPUKernel::ReSize() {
auto in_tensor = inputs_.front();
num_unit_ = static_cast<int>(in_tensor->DataSize());
thread_n_num_ = MSMIN(thread_num_, num_unit_);
thread_n_stride_ = UP_DIV(num_unit_, thread_n_num_);
return RET_OK;
}
int QuantDTypeCastCPUKernel::ReSize() { return RET_OK; }
int QuantDTypeCastCPUKernel::QuantDTypeCast(int task_id) {
int num_unit_thread = MSMIN(thread_n_stride_, num_unit_ - task_id * thread_n_stride_);
if (num_unit_thread <= 0) {
@ -108,6 +105,11 @@ int QuantDTypeCastRun(int task_id, LiteParallelGroupEnv *penv, void *cdata) {
}
int QuantDTypeCastCPUKernel::Run() {
auto prepare_ret = Prepare();
if (prepare_ret != RET_OK) {
MS_LOG(ERROR) << "Prepare fail!ret: " << prepare_ret;
return prepare_ret;
}
if (inverse_) {
int8_ptr_ = reinterpret_cast<int8_t *>(inputs_[0]->Data());
float32_ptr_ = reinterpret_cast<float *>(outputs_[0]->Data());

@ -28,10 +28,7 @@ using mindspore::lite::RET_OK;
using mindspore::schema::PrimitiveType_Reshape;
namespace mindspore::kernel {
int ReshapeBaseCPUKernel::Init() {
reshape_param_->thread_count_ = thread_count_;
return RET_OK;
}
int ReshapeBaseCPUKernel::Init() { return RET_OK; }
kernel::LiteKernel *CpuReshapeInt8KernelCreator(const std::vector<lite::tensor::Tensor *> &inputs,
const std::vector<lite::tensor::Tensor *> &outputs,

@ -29,7 +29,7 @@ class ReshapeBaseCPUKernel : public LiteKernel {
ReshapeBaseCPUKernel(OpParameter *parameter, const std::vector<lite::tensor::Tensor *> &inputs,
const std::vector<lite::tensor::Tensor *> &outputs, const Context *ctx,
const lite::Primitive *primitive)
: LiteKernel(parameter, inputs, outputs, ctx, primitive), ctx_(ctx), thread_count_(ctx->thread_num_) {
: LiteKernel(parameter, inputs, outputs, ctx, primitive), ctx_(ctx) {
reshape_param_ = reinterpret_cast<ReshapeParameter *>(opParameter);
}
~ReshapeBaseCPUKernel() = default;
@ -39,7 +39,6 @@ class ReshapeBaseCPUKernel : public LiteKernel {
int Run() override { return 0; }
protected:
int thread_count_;
const Context *ctx_;
ReshapeParameter *reshape_param_;
};

@ -36,7 +36,10 @@ int SoftmaxBaseCPUKernel::Init() {
MS_LOG(ERROR) << "SoftmaxParameter nullptr";
return RET_NULL_PTR;
}
return RET_OK;
}
int SoftmaxBaseCPUKernel::ReSize() {
auto input_tensor = inputs_.front();
auto in_shape = input_tensor->shape();
auto in_dims = in_shape.size();

@ -28,13 +28,12 @@ class SoftmaxBaseCPUKernel : public LiteKernel {
const std::vector<lite::tensor::Tensor *> &outputs, const lite::Context *ctx,
const lite::Primitive *primitive)
: LiteKernel(parameter, inputs, outputs, ctx, primitive), ctx_(ctx), thread_count_(ctx->thread_num_) {
opParameter->thread_num_ = ctx->thread_num_;
softmax_param_ = reinterpret_cast<SoftmaxParameter *>(opParameter);
}
~SoftmaxBaseCPUKernel() = default;
int Init() override;
int ReSize() override { return 0; }
int ReSize() override;
int Run() override { return 0; }
protected:

@ -28,7 +28,9 @@ using mindspore::lite::RET_OK;
using mindspore::schema::PrimitiveType_Split;
namespace mindspore::kernel {
int SplitBaseCPUKernel::Init() {
int SplitBaseCPUKernel::Init() { return RET_OK; }
int SplitBaseCPUKernel::ReSize() {
auto in_tensor = inputs_.front();
auto input_shape = in_tensor->shape();

@ -35,7 +35,7 @@ class SplitBaseCPUKernel : public LiteKernel {
~SplitBaseCPUKernel() = default;
int Init() override;
int ReSize() override { return 0; }
int ReSize() override;
int Run() override { return 0; }
protected:

@ -30,9 +30,7 @@ class SqueezeBaseCPUKernel : public LiteKernel {
SqueezeBaseCPUKernel(OpParameter *parameter, const std::vector<lite::tensor::Tensor *> &inputs,
const std::vector<lite::tensor::Tensor *> &outputs, const Context *ctx,
const lite::Primitive *primitive)
: LiteKernel(parameter, inputs, outputs, ctx, primitive), ctx_(ctx), thread_count_(ctx->thread_num_) {
opParameter->thread_num_ = ctx->thread_num_;
}
: LiteKernel(parameter, inputs, outputs, ctx, primitive), ctx_(ctx), thread_count_(ctx->thread_num_) {}
virtual ~SqueezeBaseCPUKernel() = default;

@ -31,6 +31,14 @@ using mindspore::schema::PrimitiveType_StridedSlice;
namespace mindspore::kernel {
int StridedSliceCPUKernel::Init() {
if (!InferShapeDone()) {
return RET_OK;
}
return ReSize();
}
int StridedSliceCPUKernel::ReSize() {
auto input = inputs_.at(0);
auto parameter = reinterpret_cast<StridedSliceParameter *>(opParameter);
MS_ASSERT(input);
@ -39,13 +47,11 @@ int StridedSliceCPUKernel::Init() {
return RET_OK;
}
int StridedSliceCPUKernel::ReSize() { return 0; }
int StridedSliceCPUKernel::Run() {
auto ret = Prepare();
if (ret != RET_OK) {
MS_LOG(ERROR) << "Prepare failed.";
return RET_ERROR;
MS_LOG(ERROR) << "Prepare fail!ret: " << ret;
return ret;
}
auto input = inputs_.at(0);

@ -27,15 +27,12 @@ class StridedSliceCPUKernel : public LiteKernel {
StridedSliceCPUKernel(OpParameter *parameter, const std::vector<lite::tensor::Tensor *> &inputs,
const std::vector<lite::tensor::Tensor *> &outputs, const lite::Context *ctx,
const lite::Primitive *primitive)
: LiteKernel(parameter, inputs, outputs, ctx, primitive), thread_num_(ctx->thread_num_) {}
: LiteKernel(parameter, inputs, outputs, ctx, primitive) {}
~StridedSliceCPUKernel() override = default;
int Init() override;
int ReSize() override;
int Run() override;
private:
int thread_num_;
};
} // namespace mindspore::kernel

@ -57,7 +57,7 @@ int AddNCPUKernel::AddNParallelRun(int thread_id) {
int AddNCPUKernel::Run() {
auto ret = Prepare();
if (ret != RET_OK) {
MS_LOG(ERROR) << "Prepare failed.";
MS_LOG(ERROR) << "Prepare fail!ret: " << ret;
return ret;
}
elements_num_ = inputs_[0]->ElementsNum();

@ -29,8 +29,7 @@ using mindspore::lite::RET_OK;
using mindspore::schema::PrimitiveType_Eltwise;
namespace mindspore::kernel {
ArithmeticCPUKernel::~ArithmeticCPUKernel() {
void ArithmeticCPUKernel::FreeTileData() {
if (tile_data0_ != nullptr) {
delete[](tile_data0_);
tile_data0_ = nullptr;
@ -40,21 +39,27 @@ ArithmeticCPUKernel::~ArithmeticCPUKernel() {
tile_data1_ = nullptr;
}
}
ArithmeticCPUKernel::~ArithmeticCPUKernel() {
FreeTileData();
}
int ArithmeticCPUKernel::Init() {
if (context_->infer_shape_interrupt_ && !context_->running_) {
SetNeedReInit();
if (!InferShapeDone()) {
return RET_OK;
}
return ReSize();
}
int ArithmeticCPUKernel::ReSize() {
FreeTileData();
auto element_num = outputs_[0]->ElementsNum();
tile_data0_ = new float[element_num];
tile_data1_ = new float[element_num];
return RET_OK;
}
int ArithmeticCPUKernel::ReSize() { return RET_OK; }
int ArithmeticCPUKernel::DoArithmetic(int task_id) {
auto input0_data = reinterpret_cast<float *>(inputs_[0]->Data());
auto input1_data1 = reinterpret_cast<float *>(inputs_[1]->Data());
@ -98,7 +103,7 @@ int ArithmeticsRun(int task_id, LiteParallelGroupEnv *penv, void *cdata) {
int ArithmeticCPUKernel::Run() {
auto ret = Prepare();
if (ret != RET_OK) {
MS_LOG(ERROR) << "Prepare failed.";
MS_LOG(ERROR) << "Prepare fail!ret: " << ret;
return ret;
}
if (arithmeticParameter_->broadcasting_) {

@ -172,6 +172,7 @@ class ArithmeticCPUKernel : public LiteKernel {
int DoArithmetic(int task_id);
private:
void FreeTileData();
int thread_count_;
float *tile_data0_ = nullptr;
float *tile_data1_ = nullptr;

@ -27,12 +27,11 @@ using mindspore::lite::RET_OK;
namespace mindspore::kernel {
int ArithmeticSelfCPUKernel::Init() {
if (context_->infer_shape_interrupt_ && !context_->running_) {
SetNeedReInit();
if (!InferShapeDone()) {
return RET_OK;
}
int ret = ReSize();
return ret;
return ReSize();
}
int ArithmeticSelfCPUKernel::ReSize() {
@ -74,7 +73,7 @@ int ArithmeticSelfCPUKernel::DoArithmeticSelf(int task_id) {
int ArithmeticSelfCPUKernel::Run() {
auto ret = Prepare();
if (ret != RET_OK) {
MS_LOG(ERROR) << "Prepare failed.";
MS_LOG(ERROR) << "Prepare fail!ret: " << ret;
return ret;
}
auto input_tensor = inputs_.at(0);

@ -28,7 +28,18 @@ using mindspore::lite::RET_OK;
using mindspore::schema::PrimitiveType_BiasAdd;
namespace mindspore::kernel {
int BiasCPUKernel::ReSize() { return RET_OK; }
int BiasCPUKernel::ReSize() {
auto dims = inputs_[0]->shape();
MS_ASSERT(dims.size() <= 5);
bias_param_->ndim_ = dims.size();
for (int i = 0; i < bias_param_->ndim_; i++) {
bias_param_->in_shape0_[i] = dims[i];
bias_param_->in_shape1_[i] = 1;
bias_param_->out_shape_[i] = dims[i];
}
bias_param_->in_shape1_[bias_param_->ndim_ - 1] = dims[bias_param_->ndim_ - 1];
return RET_OK;
}
int BiasCPUKernel::Run() {
auto prepare_ret = Prepare();
@ -49,20 +60,11 @@ int BiasCPUKernel::Run() {
}
int BiasCPUKernel::Init() {
if (context_->infer_shape_interrupt_ && !context_->running_) {
SetNeedReInit();
if (!InferShapeDone()) {
return RET_OK;
}
auto dims = inputs_[0]->shape();
MS_ASSERT(dims.size() <= 5);
bias_param_->ndim_ = dims.size();
for (int i = 0; i < bias_param_->ndim_; i++) {
bias_param_->in_shape0_[i] = dims[i];
bias_param_->in_shape1_[i] = 1;
bias_param_->out_shape_[i] = dims[i];
}
bias_param_->in_shape1_[bias_param_->ndim_ - 1] = dims[bias_param_->ndim_ - 1];
return RET_OK;
return ReSize();
}
kernel::LiteKernel *CpuBiasFp32KernelCreator(const std::vector<lite::tensor::Tensor *> &inputs,

@ -25,12 +25,7 @@ using mindspore::lite::RET_OK;
using mindspore::schema::PrimitiveType_BroadcastTo;
namespace mindspore::kernel {
int BroadcastToCPUKernel::Init() {
if (context_->infer_shape_interrupt_ && !context_->running_) {
SetNeedReInit();
return RET_OK;
}
int BroadcastToCPUKernel::ReSize() {
auto input_shape = inputs_[0]->shape();
for (size_t i = 0; i < input_shape.size(); ++i) {
shape_info_.input_shape_[i] = input_shape[i];
@ -45,6 +40,14 @@ int BroadcastToCPUKernel::Init() {
return RET_OK;
}
int BroadcastToCPUKernel::Init() {
if (!InferShapeDone()) {
return RET_OK;
}
return ReSize();
}
int BroadcastToCPUKernel::Run() {
auto prepare_ret = Prepare();
if (prepare_ret != RET_OK) {

@ -31,7 +31,7 @@ class BroadcastToCPUKernel : public LiteKernel {
~BroadcastToCPUKernel() = default;
int Init() override;
int ReSize() override { return 0; }
int ReSize() override;
int Run() override;
private:

@ -41,10 +41,13 @@ int CastRun(int thread_id, LiteParallelGroupEnv *penv, void *cdata) {
} // namespace
int CastCPUKernel::Init() {
if (context_->infer_shape_interrupt_ && !context_->running_) {
SetNeedReInit();
if (!InferShapeDone()) {
return RET_OK;
}
return ReSize();
}
int CastCPUKernel::ReSize() {
data_num_ = inputs_[0]->ElementsNum();
if (data_num_ == 0) {
return RET_OK;

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save