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

#include <numeric>
#include <vector>
#include <cmath>
#include <memory>
#include <vector>

#include "aidge/utils/Registrar.hpp"
#include "aidge/operator/OperatorTensor.hpp"
#include "aidge/data/Tensor.hpp"
#include "aidge/graph/Node.hpp"
#include "aidge/utils/StaticAttributes.hpp"
#include "aidge/utils/Types.h"

namespace Aidge {
enum class ConcatAttr { Axis };

class Concat_Op : public OperatorTensor,
    public Registrable<Concat_Op, std::string, std::unique_ptr<OperatorImpl>(const Concat_Op&)>,
    public StaticAttributes<ConcatAttr, DimSize_t> {
public:
    static const std::string Type;

    using Attributes_ = StaticAttributes<ConcatAttr, DimSize_t>;
    template <ConcatAttr e>
    using attr = typename Attributes_::template attr<e>;

    Concat_Op(const IOIndex_t nbIn, const DimSize_t axis)
        : OperatorTensor(Type, nbIn, 0, 1),
          Attributes_(attr<ConcatAttr::Axis>(axis))
    {
        if (nbIn == 0) {
            AIDGE_THROW_OR_ABORT(std::runtime_error, "Add operator should have at least one input.");
        }
    }

    /**
     * @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.
     */
    Concat_Op(const Concat_Op& op)
        : OperatorTensor(op),
          Attributes_(op)
    {
        mImpl = op.mImpl ? Registrar<Concat_Op>::create(op.mOutputs[0]->getImpl()->backend())(*this) : nullptr;
    }

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

    // Data operator[](const char* inputName) override final {
    //     std::shared_ptr<Tensor> in = (strcmp(inputName, "data")) ? mInputs[0] :
    //         (strcmp(inputName, "weight") ? mInputs[1] :
    //         (strcmp(inputName, "bias") ? mInputs[2] :
    //         nullptr));
    //     assert((in!=nullptr) && "No such parameter");
    //     return *in;
    // }


    void computeOutputDims() override final {
        // Every input is non-empty with the same number of dimensions
        bool associated = (getInput(0) != nullptr);
        associated &= !(getInput(0)->empty()) && (getAttr<ConcatAttr::Axis>() < getInput(0)->nbDims()); // do not compute anything if no input
        auto outputDims =  getInput(0)->dims();
        const auto firstInputNbDims = getInput(0) -> nbDims();
        for (IOIndex_t i = 1; i < nbInputs(); ++i) {
            if (!getInput(i)) {
                AIDGE_THROW_OR_ABORT(std::runtime_error, "Every input should be associated with a Tensor");
            }
            associated &= (getInput(i)->nbDims() == firstInputNbDims);
            for (DimSize_t dim = 0; dim < firstInputNbDims; ++dim) {
                if (dim == getAttr<ConcatAttr::Axis>()) {
                    outputDims[dim] += getInput(i)->dims()[dim];
                }
                else {
                    associated &= (getInput(i)->dims()[dim] == outputDims[dim]);
                }
            }
        }
        if (associated) {
            getOutput(0)->resize(outputDims);
        }
    }

    void setBackend(const std::string& name, DeviceIdx_t device = 0) override {
        mImpl = Registrar<Concat_Op>::create(name)(*this);
        mOutputs[0]->setBackend(name, device);
    }

    static const std::vector<std::string> getInputsName(){
        return {"data_input_0", "data_input_n"};
    }
    static const std::vector<std::string> getOutputsName(){
        return {"data_output"};
    }
};

inline std::shared_ptr<Node> Concat(const IOIndex_t nbIn, const DimIdx_t axis = 0, const std::string& name = "") {
    return std::make_shared<Node>(std::make_shared<Concat_Op>(nbIn, axis), name);
}
}

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

#endif /* AIDGE_CORE_OPERATOR_CONCAT_H_ */