Skip to content

Support step attribute in slice from Onnx #5454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions lib/Importer/ONNXModelLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1445,8 +1445,31 @@ Error ONNXModelLoader::loadSlice(const ONNX_NAMESPACE::NodeProto &op,
axesC->getType()->getElementName().str().c_str())));
}

RETURN_ERR_IF_NOT(op.input_size() == 4,
opErrMsg(op, "Steps is not currently supported!"));
if (op.input_size() > 4) {
std::vector<ssize_t> step;
Constant *stepC = getConstantByNameOrNull(op.input(4));

RETURN_ERR_IF_NOT(stepC, opErrMsg(op, "Step tensor is not Constant."));

if (stepC->getElementType() == ElemKind::Int64ITy) {
helperSetter<int64_t>(stepC, step);
} else if (stepC->getElementType() == ElemKind::Int32ITy) {
helperSetter<int32_t>(stepC, step);
} else {
RETURN_ERR_IF_NOT(
false,
opErrMsg(
op,
strFormat("Step Tensor has unsupported type '%s'",
stepC->getType()->getElementName().str().c_str())));
}

// Step is interpreted 1 as default.
for (size_t i = 0; i < step.size(); i++) {
RETURN_ERR_IF_NOT(step[i] == 1,
opErrMsg(op, "step!=1 is currently not supported"));
}
}
}
} else {
// Attributes 'starts' and 'ends' are mandatory and must be consistent.
Expand Down
69 changes: 69 additions & 0 deletions tests/models/onnxModels/sliceWithStep.onnxtxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
ir_version: 5
producer_name: "test4glow"
opset_import {
version: 10
}

graph {
name: "test-model"
input {
name: "data"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 3
}
dim {
dim_value: 3
}
dim {
dim_value: 3
}
}
}
}
}
initializer {
dims: 4
data_type: 7
name: "starts"
raw_data: "\000\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000"
}
initializer {
dims: 4
data_type: 7
name: "ends"
raw_data: "\002\000\000\000\000\000\000\000\002\000\000\000\000\000\000\000\003\000\000\000\000\000\000\000\003\000\000\000\000\000\000\000"
}
initializer {
dims: 4
data_type: 7
name: "axes"
raw_data: "\000\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\002\000\000\000\000\000\000\000\003\000\000\000\000\000\000\000"
}
initializer {
dims: 4
data_type: 7
name: "step"
raw_data: "\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000"
}
node {
input: "data"
input: "starts"
input: "ends"
input: "axes"
input: "step"
output: "out"
name: "DynamicSlice"
op_type: "Slice"
domain: ""
}
output {
name: "out"
}
}
69 changes: 69 additions & 0 deletions tests/models/onnxModels/sliceWithUnsupportedStep.onnxtxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
ir_version: 5
producer_name: "test4glow"
opset_import {
version: 10
}

graph {
name: "test-model"
input {
name: "data"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 3
}
dim {
dim_value: 3
}
dim {
dim_value: 3
}
}
}
}
}
initializer {
dims: 4
data_type: 7
name: "starts"
raw_data: "\000\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000"
}
initializer {
dims: 4
data_type: 7
name: "ends"
raw_data: "\002\000\000\000\000\000\000\000\002\000\000\000\000\000\000\000\003\000\000\000\000\000\000\000\003\000\000\000\000\000\000\000"
}
initializer {
dims: 4
data_type: 7
name: "axes"
raw_data: "\000\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\002\000\000\000\000\000\000\000\003\000\000\000\000\000\000\000"
}
initializer {
dims: 4
data_type: 7
name: "step"
raw_data: "\002\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000"
}
node {
input: "data"
input: "starts"
input: "ends"
input: "axes"
input: "step"
output: "out"
name: "DynamicSlice"
op_type: "Slice"
domain: ""
}
output {
name: "out"
}
}
3 changes: 2 additions & 1 deletion tests/unittests/OnnxExporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ TEST(exporter, onnxModels) {
name.find("pow_scalar_broadcast.onnxtxt") != std::string::npos ||
name.find("simpleConvTransposeAutoPadSameUpper.onnxtxt") !=
std::string::npos ||
name.find("sliceInvalidAxes.onnxtxt") != std::string::npos) {
name.find("sliceInvalidAxes.onnxtxt") != std::string::npos ||
name.find("sliceWithUnsupportedStep.onnxtxt") != std::string::npos) {
// Ignore invalid ONNX files and graphs without nodes.
llvm::outs() << "Ignore invalid input files: " << name << "\n";
continue;
Expand Down
13 changes: 13 additions & 0 deletions tests/unittests/OnnxImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2750,6 +2750,19 @@ TEST_F(OnnxImporterTest, importSliceInvalidAxes) {
{2, 1, 2, 2} /* output */, true);
}

TEST_F(OnnxImporterTest, importSliceWithStep) {
importSliceTest("sliceWithStep.onnxtxt", "data", {2, 3, 3, 3} /* input */,
{0, 1, 1, 1} /* starts */, /* ends: {2, 2, 3, 3} */
{2, 1, 2, 2} /* output */);
}

TEST_F(OnnxImporterTest, importSliceWithUnsupportedStep) {
importSliceTest("sliceWithUnsupportedStep.onnxtxt", "data",
{2, 3, 3, 3} /* input */,
{0, 1, 1, 1} /* starts */, /* ends: {2, 2, 3, 3} */
{2, 1, 2, 2} /* output */, true);
}

static void importCast(llvm::StringRef fileName, llvm::StringRef inputName,
llvm::ArrayRef<dim_t> inputShape, ElemKind outputKind) {
ExecutionEngine EE{};
Expand Down