fix convert int32 overflow and wrong height in resize

This commit is contained in:
zhaozhenlong 2021-01-06 20:59:29 +08:00
parent 2145757ced
commit 826ca89aad
2 changed files with 8 additions and 6 deletions

View File

@ -62,12 +62,12 @@ int ResizeBaseCPUKernel::CheckParameters() {
MS_LOG(INFO) << "Out shape is not assigned"; MS_LOG(INFO) << "Out shape is not assigned";
const_shape_ = false; const_shape_ = false;
} else { } else {
new_height_ = reinterpret_cast<int32_t *>(out_shape)[0]; new_height_ = out_tensors_.at(0)->shape()[1];
if (new_height_ < 1) { if (new_height_ < 1) {
MS_LOG(ERROR) << "Resize new_height should >= 1, but got " << new_height_; MS_LOG(ERROR) << "Resize new_height should >= 1, but got " << new_height_;
return RET_INVALID_OP_ATTR; return RET_INVALID_OP_ATTR;
} }
new_width_ = reinterpret_cast<int32_t *>(out_shape)[1]; new_width_ = out_tensors_.at(0)->shape()[2];
if (new_width_ < 1) { if (new_width_ < 1) {
MS_LOG(ERROR) << "Resize new_width should >= 1, but got " << new_width_; MS_LOG(ERROR) << "Resize new_width should >= 1, but got " << new_width_;
return RET_INVALID_OP_ATTR; return RET_INVALID_OP_ATTR;

View File

@ -15,6 +15,7 @@
*/ */
#include "tools/converter/parser/onnx/onnx_slice_parser.h" #include "tools/converter/parser/onnx/onnx_slice_parser.h"
#include <algorithm>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <numeric> #include <numeric>
@ -36,31 +37,32 @@ lite::PrimitiveC *OnnxSliceParser::ParseLitePrimitive(const onnx::GraphProto &on
std::vector<int> ends; std::vector<int> ends;
std::vector<int> axes; std::vector<int> axes;
std::vector<int> steps; std::vector<int> steps;
constexpr int64_t int_32_max = INT32_MAX;
for (const auto &onnx_node_attr : onnx_node.attribute()) { for (const auto &onnx_node_attr : onnx_node.attribute()) {
const auto &attribute_name = onnx_node_attr.name(); const auto &attribute_name = onnx_node_attr.name();
if (attribute_name == "starts") { if (attribute_name == "starts") {
const int num = onnx_node_attr.ints_size(); const int num = onnx_node_attr.ints_size();
starts.clear(); starts.clear();
for (int i = 0; i < num; ++i) { for (int i = 0; i < num; ++i) {
starts.push_back(static_cast<int>(onnx_node_attr.ints()[i])); starts.push_back(static_cast<int>(std::min(onnx_node_attr.ints()[i], int_32_max)));
} }
} else if (attribute_name == "axes") { } else if (attribute_name == "axes") {
const int num = onnx_node_attr.ints_size(); const int num = onnx_node_attr.ints_size();
axes.clear(); axes.clear();
for (int i = 0; i < num; ++i) { for (int i = 0; i < num; ++i) {
axes.push_back(static_cast<int>(onnx_node_attr.ints()[i])); axes.push_back(static_cast<int>(std::min(onnx_node_attr.ints()[i], int_32_max)));
} }
} else if (attribute_name == "ends") { } else if (attribute_name == "ends") {
const int num = onnx_node_attr.ints_size(); const int num = onnx_node_attr.ints_size();
ends.clear(); ends.clear();
for (int i = 0; i < num; ++i) { for (int i = 0; i < num; ++i) {
ends.push_back(static_cast<int>(onnx_node_attr.ints()[i])); ends.push_back(static_cast<int>(std::min(onnx_node_attr.ints()[i], int_32_max)));
} }
} else if (attribute_name == "steps") { } else if (attribute_name == "steps") {
const int num = onnx_node_attr.ints_size(); const int num = onnx_node_attr.ints_size();
steps.clear(); steps.clear();
for (int i = 0; i < num; ++i) { for (int i = 0; i < num; ++i) {
steps.push_back(static_cast<int>(onnx_node_attr.ints()[i])); steps.push_back(static_cast<int>(std::min(onnx_node_attr.ints()[i], int_32_max)));
} }
} }
} }