!6635 [MSLITE] Fix bug of resizeNearestNeighbor and expandDims operators.

Merge pull request !6635 from wangshaocong/lite_hub
This commit is contained in:
mindspore-ci-bot 2020-09-23 18:23:56 +08:00 committed by Gitee
commit 48a99330d4
11 changed files with 83 additions and 72 deletions

View File

@ -18,7 +18,7 @@
#include <string.h>
#include "nnacl/errorcode.h"
int ExpandDims(float *input_ptr, float *output_ptr, size_t data_size) {
int ExpandDims(void *input_ptr, void *output_ptr, size_t data_size) {
memcpy(output_ptr, input_ptr, data_size);
return NNACL_OK;
}

View File

@ -27,7 +27,7 @@ typedef struct ExpandDimsParameter {
#ifdef __cplusplus
extern "C" {
#endif
int ExpandDims(float *input_ptr, float *output_ptr, size_t data_size);
int ExpandDims(void *input_ptr, void *output_ptr, size_t data_size);
#ifdef __cplusplus
}
#endif

View File

@ -97,31 +97,13 @@ int Resize::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers:
namespace {
constexpr int kInputRank = 4;
} // namespace
template <typename T>
void CalShape(const T *data, const std::vector<Tensor *> &inputs, std::vector<int> *out_shape, int shape_size) {
int input_count = inputs[0]->ElementsNum();
int index = 0;
int size = 1;
for (int i = 0; i < shape_size; i++) {
if (static_cast<int>(data[i]) == -1) {
index = i;
} else {
size *= data[i];
}
out_shape->push_back(data[i]);
}
if (static_cast<int>(data[index]) == -1) {
(*out_shape)[index] = input_count / size;
}
}
int Resize::InferShape(std::vector<lite::Tensor *> inputs_, std::vector<lite::Tensor *> outputs_) {
MS_ASSERT(this->primitive_ != nullptr);
auto input = inputs_.front();
if (input == nullptr) {
return RET_ERROR;
}
if (input->shape().size() != kInputRank) {
if (!input->shape().empty() && input->shape().size() != kInputRank) {
MS_LOG(ERROR) << "Size of input shape is wrong.";
return RET_ERROR;
}
@ -145,31 +127,9 @@ int Resize::InferShape(std::vector<lite::Tensor *> inputs_, std::vector<lite::Te
return RET_INFER_INVALID;
}
size_t shape_size = shape_tensor->ElementsNum();
switch (shape_tensor->data_type()) {
case kNumberTypeInt8: {
auto data = reinterpret_cast<int8_t *>(shape_tensor->MutableData());
CalShape<int8_t>(data, inputs_, &output_shape, shape_size);
} break;
case kNumberTypeInt32: {
auto data = reinterpret_cast<int32_t *>(shape_tensor->MutableData());
CalShape<int32_t>(data, inputs_, &output_shape, shape_size);
} break;
case kNumberTypeInt64: {
auto data = reinterpret_cast<int64_t *>(shape_tensor->MutableData());
CalShape<int64_t>(data, inputs_, &output_shape, shape_size);
} break;
case kNumberTypeFloat: {
auto data = reinterpret_cast<float *>(shape_tensor->MutableData());
CalShape<float>(data, inputs_, &output_shape, shape_size);
} break;
case kNumberTypeUInt32: {
auto data = reinterpret_cast<uint32_t *>(shape_tensor->MutableData());
CalShape<uint32_t>(data, inputs_, &output_shape, shape_size);
} break;
default: {
MS_LOG(ERROR) << "Reshape weight tensor has unsupported dataType: " << shape_tensor->data_type();
return RET_INFER_ERR;
}
auto data = reinterpret_cast<int32_t *>(shape_tensor->data_c());
for (size_t i = 0; i < shape_size; i++) {
output_shape.push_back(data[i]);
}
} else if (inputs_.size() == kSingleNum) {
auto new_height = GetNewHeight();

View File

@ -29,7 +29,7 @@ using mindspore::lite::RET_OK;
namespace mindspore::kernel {
namespace {
constexpr int kInputNum = 1;
constexpr int kMaxInputNum = 2;
constexpr int kOutputNum = 1;
constexpr int kRank = 4;
} // namespace
@ -46,15 +46,35 @@ int ResizeBaseCPUKernel::CheckParameters() {
MS_LOG(ERROR) << "Resize method should be bilinear or nearest_neighbor, but got " << method_;
return RET_INVALID_OP_ATTR;
}
new_height_ = parameter->new_height_;
if (new_height_ < 1) {
MS_LOG(ERROR) << "Resize new_height should >= 1, but got " << new_height_;
return RET_INVALID_OP_ATTR;
}
new_width_ = parameter->new_width_;
if (new_width_ < 1) {
MS_LOG(ERROR) << "Resize new_width should >= 1, but got " << new_width_;
return RET_INVALID_OP_ATTR;
if (this->in_tensors_.size() == lite::kSingleNum) {
new_height_ = parameter->new_height_;
if (new_height_ < 1) {
MS_LOG(ERROR) << "Resize new_height should >= 1, but got " << new_height_;
return RET_INVALID_OP_ATTR;
}
new_width_ = parameter->new_width_;
if (new_width_ < 1) {
MS_LOG(ERROR) << "Resize new_width should >= 1, but got " << new_width_;
return RET_INVALID_OP_ATTR;
}
} else if (this->in_tensors_.size() == lite::kDoubleNum) {
auto out_shape = this->in_tensors_[1]->MutableData();
if (out_shape == nullptr) {
MS_LOG(INFO) << "Out shape is not assigned";
const_shape_ = false;
} else {
new_height_ = reinterpret_cast<int32_t *>(out_shape)[0];
if (new_height_ < 1) {
MS_LOG(ERROR) << "Resize new_height should >= 1, but got " << new_height_;
return RET_INVALID_OP_ATTR;
}
new_width_ = reinterpret_cast<int32_t *>(out_shape)[1];
if (new_width_ < 1) {
MS_LOG(ERROR) << "Resize new_width should >= 1, but got " << new_width_;
return RET_INVALID_OP_ATTR;
}
const_shape_ = true;
}
}
align_corners_ = parameter->align_corners_;
preserve_aspect_ratio = parameter->preserve_aspect_ratio_;
@ -66,8 +86,15 @@ int ResizeBaseCPUKernel::CheckParameters() {
}
int ResizeBaseCPUKernel::CheckInputsOuputs() {
if (in_tensors_.size() != kInputNum) {
MS_LOG(ERROR) << "Resize input num should be " << kInputNum << ", but got " << in_tensors_.size();
if (in_tensors_.size() <= lite::kDoubleNum) {
for (size_t i = 0; i < in_tensors_.size(); i++) {
auto input = in_tensors_.at(i);
if (input == nullptr) {
return RET_NULL_PTR;
}
}
} else {
MS_LOG(ERROR) << "Resize input num should be no more than" << kMaxInputNum << ", but got " << in_tensors_.size();
return RET_ERROR;
}
auto input = in_tensors_.at(0);
@ -97,7 +124,7 @@ int ResizeBaseCPUKernel::Init() {
auto input = in_tensors_.at(0);
auto input_shape = input->shape();
if (input_shape.size() != kRank) {
if (!input_shape.empty() && input_shape.size() != kRank) {
MS_LOG(ERROR) << "Resize op support input rank 4, got " << input_shape.size();
return RET_ERROR;
}

View File

@ -42,6 +42,7 @@ class ResizeBaseCPUKernel : public LiteKernel {
int64_t new_width_;
bool align_corners_;
bool preserve_aspect_ratio;
bool const_shape_;
private:
int CheckParameters();

View File

@ -48,10 +48,20 @@ int ExpandDimsCPUKernel::DoExpandDims(int task_id) {
return RET_OK;
}
int offset = task_id * thread_sz_stride_;
int ret = ExpandDims(in_ptr_ + offset, out_ptr_ + offset, size * sizeof(float));
if (ret != RET_OK) {
MS_LOG(ERROR) << "ExpandDimsRun error task_id[" << task_id << "] error_code[" << ret << "]";
return ret;
if (this->in_tensors_[0]->data_type() == kNumberTypeFloat32) {
int ret = ExpandDims(reinterpret_cast<float *>(in_ptr_) + offset,
reinterpret_cast<float *>(out_ptr_) + offset, size * sizeof(float));
if (ret != RET_OK) {
MS_LOG(ERROR) << "ExpandDimsRun error task_id[" << task_id << "] error_code[" << ret << "]";
return ret;
}
} else if (this->in_tensors_[0]->data_type() == kNumberTypeInt8) {
int ret = ExpandDims(reinterpret_cast<int8_t *>(in_ptr_) + offset,
reinterpret_cast<int8_t *>(out_ptr_) + offset, size * sizeof(int8_t));
if (ret != RET_OK) {
MS_LOG(ERROR) << "ExpandDimsRun error task_id[" << task_id << "] error_code[" << ret << "]";
return ret;
}
}
return RET_OK;
}
@ -72,8 +82,8 @@ int ExpandDimsCPUKernel::Run() {
MS_LOG(ERROR) << "Prepare fail!ret: " << prepare_ret;
return prepare_ret;
}
in_ptr_ = reinterpret_cast<float *>(in_tensors_.at(0)->MutableData());
out_ptr_ = reinterpret_cast<float *>(out_tensors_.at(0)->MutableData());
in_ptr_ = in_tensors_.at(0)->MutableData();
out_ptr_ = out_tensors_.at(0)->MutableData();
auto ret = ParallelLaunch(this->context_->thread_pool_, ExpandDimsRun, this, thread_sz_count_);
if (ret != RET_OK) {
MS_LOG(ERROR) << "ExpandDimsRun error error_code[" << ret << "]";
@ -105,4 +115,5 @@ kernel::LiteKernel *CpuExpandsDimsFp32KernelCreator(const std::vector<lite::Tens
}
REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_ExpandDims, CpuExpandsDimsFp32KernelCreator)
REG_KERNEL(kCPU, kNumberTypeInt8, PrimitiveType_ExpandDims, CpuExpandsDimsFp32KernelCreator)
} // namespace mindspore::kernel

View File

@ -44,8 +44,8 @@ class ExpandDimsCPUKernel : public LiteKernel {
int thread_sz_count_;
int thread_sz_stride_;
size_t data_size_;
float *in_ptr_;
float *out_ptr_;
void *in_ptr_;
void *out_ptr_;
int thread_count_;
};
} // namespace mindspore::kernel

View File

@ -159,12 +159,10 @@ int PadCPUKernel::CheckPaddings(int *paddings, int length, int *input_shape, int
for (auto i = 0; i < length; ++i) {
int max_valid = input_shape[i] - offset;
if (paddings[i * 2] > max_valid) {
MS_LOG(ERROR) << prefix << "paddings " << paddings[i * 2] << "should be less than " << max_valid + 1;
return RET_ERROR;
MS_LOG(WARNING) << prefix << "paddings " << paddings[i * 2] << "should be less than " << max_valid + 1;
}
if (paddings[i * 2 + 1] > max_valid) {
MS_LOG(ERROR) << prefix << "paddings " << paddings[i * 2 + 1] << "should be less than " << max_valid + 1;
return RET_ERROR;
MS_LOG(WARNING) << prefix << "paddings " << paddings[i * 2 + 1] << "should be less than " << max_valid + 1;
}
}
return RET_OK;

View File

@ -179,6 +179,17 @@ int ResizeCPUKernel::RunImpl(int task_id) {
break;
}
case static_cast<int>(schema::ResizeMethod_NEAREST_NEIGHBOR): {
if (in_tensors_.size() == lite::kDoubleNum && !const_shape_) {
auto out_shape = in_tensors_.at(1);
auto data = reinterpret_cast<int32_t *>(out_shape->MutableData());
if (data == nullptr) {
MS_LOG(ERROR) << "The out shape data is nullptr.";
return RET_NULL_PTR;
} else {
out_tensors_[0]->shape()[1] = static_cast<int64_t>(data[0]);
out_tensors_[0]->shape()[2] = static_cast<int64_t>(data[1]);
}
}
ret = ResizeNearestNeighbor(input_data, output_data, input_shape.data(), out_tensors_[0]->shape().data(),
align_corners_, task_id, context_->thread_num_);
break;

View File

@ -81,4 +81,5 @@ kernel::LiteKernel *CpuShapeFp32KernelCreator(const std::vector<lite::Tensor *>
}
REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_Shape, CpuShapeFp32KernelCreator)
REG_KERNEL(kCPU, kNumberTypeInt8, PrimitiveType_Shape, CpuShapeFp32KernelCreator)
} // namespace mindspore::kernel

View File

@ -31,7 +31,9 @@ class SqueezeInt8CPUKernel : public SqueezeBaseCPUKernel {
SqueezeInt8CPUKernel(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs,
const std::vector<lite::Tensor *> &outputs, const InnerContext *ctx,
const mindspore::lite::PrimitiveC *primitive)
: SqueezeBaseCPUKernel(parameter, inputs, outputs, ctx, primitive) {}
: SqueezeBaseCPUKernel(parameter, inputs, outputs, ctx, primitive) {
para_ = reinterpret_cast<SqueezeParameter *>(parameter);
}
~SqueezeInt8CPUKernel() override { delete quant_Squeeze_parm_; }
int Init() override;