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

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

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

namespace Aidge {
enum class MatMulAttr { OutChannels };

class MatMul_Op : public Operator,
              public Registrable<MatMul_Op,
                                 std::string,
                                 std::unique_ptr<OperatorImpl>(const MatMul_Op &)>,
              public StaticAttributes<MatMulAttr, DimSize_t> {
public:
    std::array<std::shared_ptr<Tensor>, 2> mInputs = {std::make_shared<Tensor>(), std::make_shared<Tensor>()};
    const std::shared_ptr<Tensor> mOutput = std::make_shared<Tensor>();

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

    MatMul_Op() = delete;

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

    MatMul_Op(DimSize_t out_channels)
            : Operator(Type),
            Attributes_(
                attr<MatMulAttr::OutChannels>(out_channels))
    {
        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.
     */
    MatMul_Op(const MatMul_Op& op)
        : Operator(Type),
          Attributes_(op),
          mOutput(std::make_shared<Tensor>(*op.mOutput))
    {
        // cpy-ctor
        setDatatype(op.mOutput->dataType());
        mImpl = op.mImpl ? Registrar<MatMul_Op>::create(mOutput->getImpl()->backend())(*this) : nullptr;
    }
    /**
     * @brief Clone the operator using its copy-constructor.
     * @see Operator::MatMul_Op
     */
    std::shared_ptr<Operator> clone() const override {
        return std::make_shared<MatMul_Op>(*this);
    }

    void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final {
        assert(inputIdx < 2 && "operators supports only 2 inputs");
        assert(strcmp(data->type(), Tensor::Type)==0 && "input data must be of Tensor type");
        mInputs[inputIdx] = std::dynamic_pointer_cast<Tensor>(data);
    }

    void computeOutputDims() override final {
        if (!mInputs[0]->empty()) {
            // <in_features**, out_channels>
            std::array<DimSize_t, 2> weightDims = {this->template getAttr<MatMulAttr::OutChannels>(), static_cast<DimSize_t>(mInputs[0]->sizeM1())};
            // <out_channels, batch>
            std::array<DimSize_t, 2> outputDims = {mInputs[0]->dims()[0], this->template getAttr<MatMulAttr::OutChannels>()};

            mInputs[1]->resize(weightDims);
            mOutput->resize(outputDims);
        }
    }

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


    inline Tensor& input(const IOIndex_t inputIdx) const override final {
        assert(inputIdx < 2 && "operators supports only 2 inputs");
        return *(mInputs[inputIdx].get()); }
    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(inputIdx < 2 && "MatMul Operators has 2 inputs");
        return mInputs[inputIdx];
    }
    inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final {
        assert((outputIdx == 0) && "MatMul Operators has 1 output");
        (void) outputIdx; // avoid unused warning
        return mOutput;
    }


    std::shared_ptr<Data> getRawInput(const IOIndex_t inputIdx) const override final {
        assert(inputIdx < 2 && "operators supports only 2 inputs");
        return std::static_pointer_cast<Data>(mInputs[inputIdx]);
    }
    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);
    }


    void setBackend(const std::string& name) {
        mImpl = Registrar<MatMul_Op>::create(name)(*this);
        mOutput->setBackend(name);

        // FIXME: temporary workaround
        mInputs[0]->setBackend(name);
        mInputs[1]->setBackend(name);
    }

    void setDatatype(const DataType& datatype) {
        mOutput->setDatatype(datatype);

        // FIXME: temporary workaround
        mInputs[0]->setDatatype(datatype);
        mInputs[1]->setDatatype(datatype);
    }


    inline IOIndex_t nbInputs() const noexcept override final { return 2; }
    inline IOIndex_t nbDataInputs() const noexcept override final { return 1; }
    inline IOIndex_t nbOutputs() const noexcept override final { return 1; }
};

inline std::shared_ptr<Node> MatMul(DimSize_t out_channels, const std::string& name = "") {
    // FIXME: properly handle default w initialization in every cases
    auto matmul = std::make_shared<Node>(std::make_shared<MatMul_Op>(out_channels), name);
    addProducer(matmul, 1, {out_channels, 1}, "w");
    return matmul;
}
} // namespace Aidge

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

#endif /* AIDGE_CORE_OPERATOR__MATMUL_H_ */