Skip to content
Snippets Groups Projects
Forked from Eclipse Projects / aidge / aidge_core
2145 commits behind the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Scaling.hpp 5.59 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_Scaling_H__
#define __AIDGE_CORE_OPERATOR_Scaling_H__

#include <vector>
#include <memory>



#include "aidge/utils/StaticAttributes.hpp"
#include "aidge/utils/Registrar.hpp"
#include "aidge/operator/Operator.hpp"
#include "aidge/backend/OperatorImpl.hpp"
#include "aidge/data/Tensor.hpp"
#include "aidge/data/Data.hpp"
#include "aidge/graph/Node.hpp"
#include "aidge/utils/Types.h"

namespace Aidge {
enum class ScalingAttr {
    scalingFactor
};

class Scaling_Op : public Operator,
    public Registrable<Scaling_Op, std::string, std::unique_ptr<OperatorImpl>(const Scaling_Op&)>,
    public StaticAttributes<ScalingAttr, float> {
public:
    // FIXME: change accessibility
    std::shared_ptr<Tensor> mInput = std::make_shared<Tensor>();
    const std::shared_ptr<Tensor> mOutput = std::make_shared<Tensor>();

public:
    static constexpr const char* Type = "Scaling";

    Scaling_Op() = delete;

    using Attributes_ = StaticAttributes<ScalingAttr, float>;
    template <ScalingAttr e> using attr = typename Attributes_::template attr<e>;

    Scaling_Op(float scalingFactor)
            : Operator(Type),
            Attributes_(
                attr<ScalingAttr::scalingFactor>(scalingFactor))
    {
        setDatatype(DataType::Float32);
    }

    /**
     * @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.
     */
    Scaling_Op(const Scaling_Op& op)
        : Operator(Type),
          Attributes_(op),
          mOutput(std::make_shared<Tensor>(*op.mOutput))
    {
        // cpy-ctor
        setDatatype(op.mOutput->dataType());
        mImpl = op.mImpl ? Registrar<Scaling_Op>::create(mOutput->getImpl()->backend())(*this) : nullptr;
    }

    /**
     * @brief Clone the operator using its copy-constructor.
     * @see Operator::Scaling_Op
     */
    std::shared_ptr<Operator> clone() const override {
        return std::make_shared<Scaling_Op>(*this);
    }

    void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final {
        assert(inputIdx == 0 && "operator supports only 1 input");
        assert(strcmp(data->type(), Tensor::Type)==0 && "input data must be of Tensor type");
        (void) inputIdx; //avoid unused warning
        mInput = std::dynamic_pointer_cast<Tensor>(data);
    }

    void computeOutputDims() override final {
        if (!mInput->empty())
            mOutput->resize(mInput->dims());
    }

    bool outputDimsForwarded() const override final {
        return !(mOutput->empty());
    }


    inline Tensor& input(const IOIndex_t inputIdx) const override final {
        assert((inputIdx == 0) && "Scaling Operator has only 1 input");
        (void) inputIdx; // avoid unused warning
        return *(mInput.get());
    }
    inline Tensor& output(const IOIndex_t outputIdx) const override final {
        assert((outputIdx == 0) && "Scaling Operator has only 1 output");
        (void) outputIdx; // avoid unused warning
        return *(mOutput.get());
    }


    inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final {
        assert((inputIdx == 0) && "Scaling Operator has only 1 input");
        (void) inputIdx; // avoid unused warning
        return mInput;
    }
    inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final {
        assert((outputIdx == 0) && "Scaling 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(inputIdx == 0 && "operator supports only 1 input");
        (void) inputIdx; // avoid unused warning
        return std::static_pointer_cast<Data>(mInput);
    }
    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 mOutput;
    }


    void setBackend(const std::string& name) override {
        mImpl = Registrar<Scaling_Op>::create(name)(*this);
        mOutput->setBackend(name);
        // FIXME: temporary workaround
        mInput->setBackend(name);
    }
    void setDatatype(const DataType& datatype) override {
        mOutput->setDatatype(datatype);

        // FIXME: temporary workaround
        mInput->setDatatype(datatype);
    }

    inline IOIndex_t nbInputs() const noexcept override final { return 1; }
    inline IOIndex_t nbDataInputs() const noexcept override final { return 1; }
    inline IOIndex_t nbOutputs() const noexcept override final { return 1; }
    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> Scaling(float scalingFactor = 1.0f, const std::string& name = "") {
    return std::make_shared<Node>(std::make_shared<Scaling_Op>(scalingFactor), name);
}
}

namespace {
template <>
const char* const EnumStrings<Aidge::ScalingAttr>::data[]
    = {"scalingFactor"};
}

#endif /* __AIDGE_CORE_OPERATOR_RELU_H__ */