Forked from
Eclipse Projects / aidge / aidge_core
2045 commits behind the upstream repository.
-
Maxence Naud authoredMaxence Naud authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Slice.hpp 4.92 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
*
********************************************************************************/
#ifndef AIDGE_CORE_OPERATOR_SLICE_H_
#define AIDGE_CORE_OPERATOR_SLICE_H_
#include <memory>
#include <vector>
#include "aidge/backend/OperatorImpl.hpp"
#include "aidge/data/Tensor.hpp"
#include "aidge/graph/Node.hpp"
#include "aidge/operator/OperatorTensor.hpp"
#include "aidge/utils/Registrar.hpp"
#include "aidge/utils/StaticAttributes.hpp"
#include "aidge/utils/Types.h"
namespace Aidge {
enum class SliceAttr { Beginning, SliceDims };
template <DimIdx_t DIM>
class Slice_Op
: public OperatorTensor,
public Registrable<Slice_Op<DIM>, std::string, std::unique_ptr<OperatorImpl>(const Slice_Op<DIM> &)>,
public StaticAttributes<SliceAttr, std::size_t, std::array<DimSize_t, DIM>> {
public:
static const std::string Type;
Slice_Op() = delete;
using Attributes_ = StaticAttributes<SliceAttr, std::size_t, std::array<DimSize_t, DIM>>;
template <SliceAttr e>
using attr = typename Attributes_::template attr<e>;
Slice_Op(std::size_t beginningPos, std::array<DimSize_t, DIM> sliceDims)
: OperatorTensor(Type, 1, 0, 1),
Attributes_(attr<SliceAttr::Beginning>(beginningPos),
attr<SliceAttr::SliceDims>(sliceDims))
{}
/**
* @brief Copy-constructor. Copy the operator attributes and its output tensor(s), but not its
* input tensors (the new operator has no input associated).
* @param op Operator to copy.
*/
Slice_Op(const Slice_Op &op)
: OperatorTensor(op),
Attributes_(op)
{
mImpl = op.mImpl ? Registrar<Slice_Op<DIM>>::create(mOutputs[0]->getImpl()->backend())(*this)
: nullptr;
}
public:
/**
* @brief Clone the operator using its copy-constructor.
* @see Operator::Slice_Op
*/
std::shared_ptr<Operator> clone() const override { return std::make_shared<Slice_Op>(*this); }
void computeOutputDims() override final {
if (!getInput(0) || (getInput(0)->empty())) {
AIDGE_THROW_OR_ABORT(std::runtime_error, "Every input should be associated with a Tensor");
}
// Check input dimensions is compatible with slice dimensions
if (getInput(0)->nbDims() != DIM) {
AIDGE_THROW_OR_ABORT(std::runtime_error, "Error: input and slice dimensions are not the same size.");
}
std::array<DimSize_t, DIM> outputDims;
const std::array<DimSize_t, DIM> inputDims = getInput(0)->template dims<DIM>();
// Check that the sliced Tensor is actually part of the input Tensor
// For a 5*5 tensor ('x') and a 3*3 slice kernel ('o'):
// xxxxx xxxxx
// xxxxx xxxxx
// xxooo --> ok xxxoo --> out of bound
// xxooo xxxoo
// xxooo xxxoo
std::vector<std::size_t> beginningCoords = mInputs[0]->getCoord(this->template getAttr<SliceAttr::Beginning>());
for (std::size_t i = 0; i < DIM; ++i) {
if (beginningCoords[i] + this->template getAttr<SliceAttr::SliceDims>()[i] > inputDims[i]) {
AIDGE_THROW_OR_ABORT(std::runtime_error, "ROI of Slice operator out of bounds");
} else {
outputDims[i] = this->template getAttr<SliceAttr::SliceDims>()[i];
}
}
mOutputs[0]->resize(outputDims);
}
void setBackend(const std::string &name) {
mImpl = Registrar<Slice_Op>::create(name)(*this);
mOutputs[0]->setBackend(name);
// FIXME: temporary workaround
getInput(0)->setBackend(name);
}
static const std::vector<std::string> getInputsName(){
return {"data_input"};
}
static const std::vector<std::string> getOutputsName(){
return {"data_output"};
}
};
template <DimIdx_t DIM>
const std::string Slice_Op<DIM>::Type = "Slice";
template <std::size_t DIM>
inline std::shared_ptr<Node> Slice(std::size_t beginningPos, std::array<DimSize_t, DIM> sliceDims,
const std::string &name = "") {
// FIXME: properly handle default w&b initialization in every cases
return std::make_shared<Node>(std::make_shared<Slice_Op<DIM>>( beginningPos, sliceDims), name);
}
template <DimIdx_t DIM>
inline std::shared_ptr<Node> Slice(std::size_t beginningPos, DimSize_t const (&sliceDims)[DIM], const std::string& name = "") {
return Slice(beginningPos, to_array(sliceDims), name);
}
} // namespace Aidge
namespace {
template <>
const char *const EnumStrings<Aidge::SliceAttr>::data[] = { "Beginning", "SliceDims" };
}
#endif /* AIDGE_CORE_OPERATOR_RELU_H_ */