Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Slice.cpp 1.93 KiB
/********************************************************************************
 * Copyright (c) 2023 CEA-List
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 ********************************************************************************/
#include <cassert>
#include <cstddef>
#include <string>
#include <vector>
#include <utility>

#include "aidge/backend/OperatorImpl.hpp"
#include "aidge/operator/Slice.hpp"
#include "aidge/utils/Types.h"
#include "aidge/utils/ErrorHandling.hpp"

const std::string Aidge::Slice_Op::Type = "Slice";

void Aidge::Slice_Op::computeOutputDims() {
    // check input have been associated
    if (!getInput(0) || (getInput(0)->empty())) {
        AIDGE_THROW_OR_ABORT(std::runtime_error, "Every input should be associated with a Tensor");
    }

    DimSize_t nbAxes = this->template getAttr<SliceAttr::Axes>().size();
    std::vector<DimSize_t> outDims = getInput(0)->dims();
    for(std::size_t i=0; i<nbAxes;++i)
    {
        // For each slice operation get the params and cast them to size_t
        std::int64_t axis_ = this->template getAttr<SliceAttr::Axes>()[i];
        std::int64_t start_ = this->template getAttr<SliceAttr::Starts>()[i];
        std::int64_t end_ = this->template getAttr<SliceAttr::Ends>()[i];
        std::size_t axis = axis_>=0?axis_:axis_+getInput(0)->nbDims();
        std::size_t start = start_>=0?start_:start_+getInput(0)->dims()[axis];
        std::size_t end = end_>=0?end_:end_+getInput(0)->dims()[axis];

        std::size_t sliceLength = end - start + 1;
        // Check if slice length is valid
        if(sliceLength>getInput(0)->dims()[axis])
            AIDGE_THROW_OR_ABORT(std::runtime_error, "ROI of Slice operator out of bounds");
        outDims[axis] = sliceLength;
    }
    mOutputs[0]->resize(outDims);
}