* limitations under the License.
*/
-#include <vector>
-#include <fstream>
-#include <sstream>
-#include <cassert>
-
#include "caffe2_importer.h"
#include "passes/common_frontend/op_creator_helper.h"
#include "passes/common_frontend/shape_helper.h"
#include "caffe2_proto_helper.h"
+#include <vector>
+#include <fstream>
+#include <sstream>
+#include <cassert>
+
namespace nnc {
using namespace ::caffe2;
outputs = _opCreator->convertRelu(inputs);
break;
case SupportedCaffe2OpType::resizeNearest:
- outputs = _opCreator->convertResize(inputs, op);
+ outputs = _opCreator->convertResizeNearest(inputs, op);
break;
case SupportedCaffe2OpType::sigmoid:
outputs = _opCreator->convertSigmoid(inputs);
return {relu->getOutput(0)};
}
-std::vector<IODescriptor> Caffe2OpCreator::convertResize(const std::vector<IODescriptor>& inputs,
- const ::caffe2::OperatorDef& op) {
+std::vector<IODescriptor>
+Caffe2OpCreator::convertResizeNearest(const std::vector<IODescriptor>& inputs,
+ const ::caffe2::OperatorDef& op) {
// assume NCHW and convert to MIR (NHWC)
std::vector<float> scales(4);
assert(inputs[0].op->getOutputShape(0).rank() == 4 && "only 4d tensors is supported");
scales[1] = getSingleArgument(op, "height_scale", 1.0f);
scales[2] = getSingleArgument(op, "width_scale", 1.0f);
scales[3] = 1;
- auto resize = createOp<ops::ResizeOp>(
- convertCaffeToMIR(inputs[0]), ops::ResizeOp::ResizeMethod::nearestNeighbor, scales);
+ auto resize = createOp<ops::ResizeOp>("ResizeNearest", convertCaffeToMIR(inputs[0]),
+ ops::ResizeOp::ResizeMethod::nearestNeighbor, scales);
return {convertMIRToCaffe(resize->getOutput(0))};
}
using namespace mir;
-inline static const onnx::AttributeProto* findAttribute(const onnx::NodeProto& onnx_node,
- const std::string& name) {
+static const onnx::AttributeProto* findAttribute(const onnx::NodeProto& onnx_node,
+ const std::string& name) {
for (auto& att : onnx_node.attribute()) {
if (!att.name().compare(name)) {
return &att;
return nullptr;
}
-inline static std::pair<bool, int> getIntAttribute(const onnx::NodeProto& onnx_node,
- const std::string& name = "axis") {
+static std::pair<bool, int> getIntAttribute(const onnx::NodeProto& onnx_node,
+ const std::string& name = "axis") {
auto result = findAttribute(onnx_node, name);
if (!result)
return {false, 0};
return {true, result->i()};
}
-inline static std::pair<bool, std::string> getStringAttribute(const onnx::NodeProto& onnx_node,
- const std::string& name) {
+static std::pair<bool, std::string> getStringAttribute(const onnx::NodeProto& onnx_node,
+ const std::string& name) {
auto result = findAttribute(onnx_node, name);
if (!result)
return {false, ""};
return {true, result->s()};
}
-inline static std::pair<bool, float> getFloatAttribute(const onnx::NodeProto& onnx_node,
- const std::string& name) {
+static std::pair<bool, float> getFloatAttribute(const onnx::NodeProto& onnx_node,
+ const std::string& name) {
auto result = findAttribute(onnx_node, name);
if (!result)
return {false, 0.0};
cdata.strides_shape = ShapeHelper::createShape(strides->ints(), strides->ints_size());
if (pads) {
- assert(pads->ints_size() >= 2);
+ assert(pads->ints_size() == 4);
cdata.padding_before[0] = pads->ints(0);
cdata.padding_before[1] = pads->ints(1);
// TODO: ONNX padding could be for the beginning and ending along each axis that's why we
// should select the interesting ones.
- if (pads->ints_size() == 4) {
- cdata.padding_after[0] = pads->ints(2);
- cdata.padding_after[1] = pads->ints(3);
- }
+ cdata.padding_after[0] = pads->ints(2);
+ cdata.padding_after[1] = pads->ints(3);
}
};
std::vector<IODescriptor>
ONNXOpCreator::convertUpsample(const std::vector<mir::IODescriptor>& inputs,
- const onnx::NodeProto& node) {
+ const onnx::NodeProto& node) {
bool success;
std::string mode;
std::tie(success, mode) = getStringAttribute(node, "mode");
if (!success) mode = "nearest";
assert(mode == "nearest" && "Unsupported upscale mode!");
- assert(inputs.size() > 1); // relies on constants being lifted to initializer list
+ // relies on attributes being lifted to constants (ONNX optimization pass)
+ assert(inputs.size() > 1);
auto* scales = dynamic_cast<mir::ops::ConstantOp*>(inputs[1].op);
assert(scales && "Weights could be a constant tensor only");
auto scales_tensor = Tensor<float>(scales->getValue());