diff --git a/include/aidge/operator/Pop.hpp b/include/aidge/operator/Pop.hpp new file mode 100644 index 0000000000000000000000000000000000000000..cb4ba871a55b9dfd1c835c05949c3c18966b7f5a --- /dev/null +++ b/include/aidge/operator/Pop.hpp @@ -0,0 +1,95 @@ +/******************************************************************************** + * 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_POP_H_ +#define AIDGE_CORE_OPERATOR_POP_H_ + +#include <cassert> +#include <memory> +#include <vector> + +#include "aidge/utils/Registrar.hpp" +#include "aidge/operator/OperatorTensor.hpp" +#include "aidge/backend/OperatorImpl.hpp" +#include "aidge/data/Tensor.hpp" +#include "aidge/graph/Node.hpp" +#include "aidge/utils/Types.h" +#include "aidge/utils/StaticAttributes.hpp" + +namespace Aidge { +enum class PopAttr { ForwardStep }; + +class Pop_Op : public OperatorTensor, + public Registrable<Pop_Op, std::string, std::unique_ptr<OperatorImpl>(const Pop_Op&)>, + public StaticAttributes<PopAttr, unsigned int> { +public: + static const std::string Type; + + using Attributes_ = StaticAttributes<PopAttr, unsigned int>; + template <PopAttr e> + using attr = typename Attributes_::template attr<e>; + + Pop_Op() + : OperatorTensor(Type, 1, 0, 1), + Attributes_(attr<PopAttr::ForwardStep>(0)) + { + + } + + /** + * @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. + */ + Pop_Op(const Pop_Op& op) + : OperatorTensor(op), + Attributes_(op) + { + mImpl = op.mImpl ? Registrar<Pop_Op>::create(op.mOutputs[0]->getImpl()->backend())(*this) : nullptr; + } + + /** + * @brief Clone the operator using its copy-constructor. + * @see Operator::Pop_Op + */ + std::shared_ptr<Operator> clone() const override { + return std::make_shared<Pop_Op>(*this); + } + + void setBackend(const std::string& name, DeviceIdx_t device = 0) override { + mImpl = Registrar<Pop_Op>::create({name})(*this); + mOutputs[0]->setBackend(name, device); + } + + void computeOutputDims() override final; + void updateConsummerProducer() override; + void forward() override; + + static const std::vector<std::string> getInputsName(){ + return {"data_input"}; + } + static const std::vector<std::string> getOutputsName(){ + return {"data_output"}; + } +}; + +inline std::shared_ptr<Node> Pop(const std::string& name = "") { + return std::make_shared<Node>(std::make_shared<Pop_Op>(), name); +} +} // namespace Aidge + +namespace { +template <> +const char *const EnumStrings<Aidge::PopAttr>::data[] = { + "ForwardStep" +}; +} + +#endif /* AIDGE_CORE_OPERATOR_POP_H_ */ \ No newline at end of file diff --git a/src/operator/Pop.cpp b/src/operator/Pop.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3dd65eb4d34266f6e419bdc86362b8da4a55fdf0 --- /dev/null +++ b/src/operator/Pop.cpp @@ -0,0 +1,38 @@ +/******************************************************************************** + * 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 <string> + +#include "aidge/operator/Pop.hpp" + +const std::string Aidge::Pop_Op::Type = "Pop"; + +void Aidge::Pop_Op::computeOutputDims() { + // check inputs have been associated + if (!getInput(0)) { + AIDGE_THROW_OR_ABORT(std::runtime_error, "{}: input #0 should be associated with a Tensor", type()); + } + if (!(getInput(0)->empty())) { + auto inputDims = getInput(0)->dims(); + inputDims.erase(inputDims.begin()); + getOutput(0)->resize(inputDims); + } +} + +void Aidge::Pop_Op::updateConsummerProducer() { + Operator::updateConsummerProducer(); + this->template getAttr<PopAttr::ForwardStep>() = 0; +} + +void Aidge::Pop_Op::forward() { + Operator::forward(); + ++this->template getAttr<PopAttr::ForwardStep>(); +}