Skip to content
Snippets Groups Projects
Commit c830d426 authored by Cyril Moineau's avatar Cyril Moineau
Browse files

Add support for out of bound start and end attribute accordingly to ONNX standard.

parent 620f9e62
No related branches found
No related tags found
2 merge requests!318[Upd] release verision 0.5.0,!291Fix slice for out of bound start and end attributes
......@@ -31,6 +31,10 @@ public:
void forward() override;
};
// Implementation note:
// If start or end are out of bound then it takes the max value for the given axe.
// Example Slice with start=1, end=1000, axes=0 for tensor [0, 1, 2, 3]
// Will return [1, 2, 3]
enum class SliceAttr { Starts, Ends, Axes, Steps };
class Slice_Op
......
......@@ -253,12 +253,17 @@ bool Aidge::Slice_Op::forwardDims(bool allowDataDependency) {
const DimIdx_t axis = this->axes()[i] >= 0 ?
static_cast<DimIdx_t>(this->axes()[i]) :
static_cast<DimIdx_t>(this->axes()[i] + static_cast<DimIdx_t>(getInput(0)->nbDims()));
const DimSize_t start = this->starts()[i] >= 0 ?
DimSize_t start = this->starts()[i] >= 0 ?
static_cast<DimSize_t>(this->starts()[i]) :
static_cast<DimSize_t>(this->starts()[i] + static_cast<DimSize_t>(getInput(0)->dims()[axis]));
const DimSize_t end = this->ends()[i] >= 0 ?
// Clamp start to the range [0, axis_dim]
start = std::max(static_cast<DimSize_t>(0), std::min(start, getInput(0)->dims()[axis]-1));
DimSize_t end = this->ends()[i] >= 0 ?
static_cast<DimSize_t>(this->ends()[i]) :
static_cast<DimSize_t>(this->ends()[i] + static_cast<DimSize_t>(getInput(0)->dims()[axis]));
// Clamp end to the range [0, axis_dim]
end = std::max(static_cast<DimSize_t>(0), std::min(end, getInput(0)->dims()[axis]-1));
const std::int64_t step = this->steps()[i];
AIDGE_ASSERT(step != 0, "Slice_Op: Step ({}) must have a non-zero value on axis {}!", this->steps(), axis);
......@@ -309,4 +314,4 @@ std::shared_ptr<Aidge::Node> Aidge::Slice(const std::vector<std::int64_t>& start
const std::vector<std::int64_t>& steps,
const std::string &name) {
return std::make_shared<Node>(std::make_shared<Slice_Op>(starts, ends, axes, steps), name);
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment