Newer
Older
/********************************************************************************
* 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"

Maxence Naud
committed
#include "aidge/operator/OperatorTensor.hpp"
#include "aidge/utils/StaticAttributes.hpp"
#include "aidge/utils/Types.h"
namespace Aidge {
class Slice_OpImpl : public OperatorImpl {
public:
Slice_OpImpl(const Operator& op, const std::string& backend = ""): OperatorImpl(op, backend) {}
void forward() override;
};
enum class SliceAttr { Starts, Ends, Axes };

Maxence Naud
committed
: public OperatorTensor,
public Registrable<Slice_Op, std::string, std::shared_ptr<OperatorImpl>(const Slice_Op &)>,
public StaticAttributes<SliceAttr, std::vector<std::int64_t>, std::vector<std::int64_t>, std::vector<std::int8_t>> {
static const std::string Type;
Slice_Op() = delete;
using Attributes_ = StaticAttributes<SliceAttr, std::vector<std::int64_t>, std::vector<std::int64_t>, std::vector<std::int8_t>>;
template <SliceAttr e> using attr = typename Attributes_::template attr<e>;
Slice_Op(const std::vector<std::int64_t>& starts, const std::vector<std::int64_t>& ends, const std::vector<std::int8_t>& axes)
: OperatorTensor(Type, 4, 0, 1),
Attributes_(attr<SliceAttr::Starts>(starts),
attr<SliceAttr::Ends>(ends),
attr<SliceAttr::Axes>(axes))
{
mImpl = std::make_shared<Slice_OpImpl>(*this);
}
/**
* @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)

Maxence Naud
committed
SET_IMPL_MACRO(Slice_Op, *this, op.backend());
}
else {
mImpl = std::make_shared<Slice_OpImpl>(*this);
}
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); }
bool forwardDims(bool allowDataDependency = false) override final;
void setBackend(const std::string &name, DeviceIdx_t device = 0) override;

Maxence Naud
committed
static const std::vector<std::string> getInputsName(){
return {"data_input", "starts", "ends", "axes"};

Maxence Naud
committed
}
static const std::vector<std::string> getOutputsName(){
return {"data_output"};
/**
* @brief Exract a sub-Tensor from a bigger original Tensor.
* @param name Name of the Operator.
* @return std::shared_ptr<Node> A Node containing the Operator.
*/
inline std::shared_ptr<Node> Slice(const std::vector<std::int64_t>& starts = {},
const std::vector<std::int64_t>& ends = {},
const std::vector<std::int8_t>& axes = {},
const std::string &name = "") {
return std::make_shared<Node>(std::make_shared<Slice_Op>(starts, ends, axes), name);
namespace {
template <>
const char *const EnumStrings<Aidge::SliceAttr>::data[] = { "Starts", "Ends", "Axes" };
}