Forked from
Eclipse Projects / aidge / aidge_core
2841 commits behind the upstream repository.
-
Olivier BICHLER authoredOlivier BICHLER authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Producer.hpp 5.78 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_PRODUCER_H_
#define AIDGE_CORE_OPERATOR_PRODUCER_H_
#include <array>
#include <vector>
#include "aidge/utils/Types.h"
#include "aidge/data/Tensor.hpp"
#include "aidge/graph/Node.hpp"
#include "aidge/operator/Operator.hpp"
#include "aidge/utils/Parameter.hpp"
#include "aidge/utils/Registrar.hpp"
namespace Aidge {
class Producer_Op
: public Operator,
public Registrable<Producer_Op, std::string, std::unique_ptr<OperatorImpl>(
const Producer_Op &)> {
private:
std::shared_ptr<Tensor> mOutput = std::make_shared<Tensor>();
public:
static constexpr const char* Type = "Producer";
template <std::size_t DIM>
Producer_Op(const std::array<DimSize_t, DIM>& dims)
: Operator(Type)
{
//ctor
setDatatype(DataType::Float32);
mOutput->resize(dims);
}
Producer_Op(const std::shared_ptr<Tensor> tensor)
: Operator(Type),
mOutput(tensor)
{
setDatatype(tensor->dataType());
}
/**
* @brief Copy-constructor. Copy the operator parameters and its output tensor(s), but not its input tensors (the new operator has no input associated).
* @param op Operator to copy.
*/
Producer_Op(const Producer_Op& op)
: Operator(Type),
mOutput(std::make_shared<Tensor>(*op.mOutput))
{
// cpy-ctor
setDatatype(op.mOutput->dataType());
}
/**
* @brief Clone the operator using its copy-constructor.
* @see Operator::Producer_Op
*/
std::shared_ptr<Operator> clone() const override {
return std::make_shared<Producer_Op>(*this);
}
void associateInput(const IOIndex_t /*inputIdx*/, std::shared_ptr<Data> /*data*/) override final {
assert(false && "Producer operator takes no input");
}
void computeOutputDims() override final {}
bool outputDimsForwarded() const override final {return true;}
[[noreturn]] inline Tensor& input(const IOIndex_t /*inputIdx*/) const override final {
assert(false);
exit(-1);
}
inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mOutput.get()); }
inline std::shared_ptr<Tensor> getInput(const IOIndex_t /*inputIdx*/) const override final {
assert(false && "Producer Operator has no input");
return nullptr;
}
inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final {
assert((outputIdx == 0) && "Producer Operator has only 1 output");
(void) outputIdx; // avoid unused warning
return mOutput;
}
std::shared_ptr<Data> getRawInput(const IOIndex_t /*inputIdx*/) const override final {
assert(false && "Producer operator takes no input");
return nullptr;
}
std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final {
assert(outputIdx == 0 && "operator supports only 1 output");
(void) outputIdx; // avoid unused warning
return std::static_pointer_cast<Data>(mOutput);
}
inline const std::vector<DimSize_t> dims() const noexcept { return mOutput->dims(); }
void setBackend(const std::string& name) {
mImpl = Registrar<Producer_Op>::create(name)(*this);
mOutput->setBackend(name);
}
void setDatatype(const DataType& datatype) {
mOutput->setDatatype(datatype);
}
inline IOIndex_t nbInputs() const noexcept override final { return 0; };
inline IOIndex_t nbDataInputs() const noexcept override final { return 0; };
inline IOIndex_t nbOutputs() const noexcept override final { return 1; };
public:
void forward() override final {
printf("Basic Producer forward() function.\n");
}
void backward() override final {
printf("Basic Producer backward() function.\n");
}
};
template <std::array<DimSize_t, 1>::size_type DIM>
inline std::shared_ptr<Node> Producer(const std::array<DimSize_t, DIM> &dims, const std::string& name = "") {
static_assert(DIM<=MaxDim,"Too many tensor dimensions required by Producer, not supported");
return std::make_shared<Node>(std::make_shared<Producer_Op>(dims), name);
}
template <std::size_t DIM>
inline std::shared_ptr<Node> Producer(DimSize_t const (&dims)[DIM], const std::string& name = "") {
return Producer(to_array(dims), name);
}
inline std::shared_ptr<Node> Producer(const std::shared_ptr<Tensor> tensor, const std::string& name = "") {
return std::make_shared<Node>(std::make_shared<Producer_Op>(tensor), name);
}
template <std::array<DimSize_t, 1>::size_type DIM>
void addProducer(std::shared_ptr<Node>& otherNode, const IOIndex_t inputIdx, const std::array<DimSize_t, DIM>& dims, const std::string& extension) {
assert(inputIdx != gk_IODefaultIndex);
static_assert(DIM<=MaxDim,"Too many tensor dimensions required by addProducer, not supported");
const std::string prodName = (otherNode->name().empty()) ? "" : (otherNode->name() + std::string("_") + extension);
auto prod = Producer(dims, prodName);
prod->addChild(otherNode, 0, inputIdx);
otherNode->getOperator()->associateInput(inputIdx, prod->getOperator()->getRawOutput(0));
}
template <std::size_t DIM>
void addProducer(std::shared_ptr<Node>& otherNode, const IOIndex_t inputIdx, DimSize_t const (&dims)[DIM], const std::string& extension) {
addProducer(otherNode, inputIdx, to_array(dims), extension);
}
} // namespace Aidge
#endif /* AIDGE_CORE_OPERATOR_PRODUCER_H_ */