Skip to content
Snippets Groups Projects
MatMul.hpp 2.96 KiB
Newer Older
Cyril Moineau's avatar
Cyril Moineau committed
/********************************************************************************
 * 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_
Cyril Moineau's avatar
Cyril Moineau committed

#include <memory>
Cyril Moineau's avatar
Cyril Moineau committed
#include <vector>

#include "aidge/utils/Types.h"
#include "aidge/data/Tensor.hpp"
#include "aidge/graph/Node.hpp"
#include "aidge/utils/Registrar.hpp"
Cyril Moineau's avatar
Cyril Moineau committed

namespace Aidge {

Maxence Naud's avatar
Maxence Naud committed
              public Registrable<MatMul_Op,
Cyril Moineau's avatar
Cyril Moineau committed
                                 std::string,
Houssem ROUIS's avatar
Houssem ROUIS committed
                                 std::unique_ptr<OperatorImpl>(const MatMul_Op &)> {
Cyril Moineau's avatar
Cyril Moineau committed
public:
Cyril Moineau's avatar
Cyril Moineau committed

    MatMul_Op() : OperatorTensor(Type, 2, 0, 1) {}
Cyril Moineau's avatar
Cyril Moineau committed

     * @brief Copy-constructor. Copy the operator attributes and its output tensor(s), but not its input tensors (the new operator has no input associated).
Houssem ROUIS's avatar
Houssem ROUIS committed
    MatMul_Op(const MatMul_Op& op) : OperatorTensor(op)
        mImpl = op.mImpl ? Registrar<MatMul_Op>::create(op.mOutputs[0]->getImpl()->backend())(*this) : nullptr;
    }

    /**
     * @brief Clone the operator using its copy-constructor.
Cyril Moineau's avatar
Cyril Moineau committed
     * @see Operator::MatMul_Op
    std::shared_ptr<Operator> clone() const override final {
Cyril Moineau's avatar
Cyril Moineau committed
        return std::make_shared<MatMul_Op>(*this);
    /**
     * @brief Compute dimensions for the output Tensor following the same rules as
     * numpy.matmul.
     * @note - Both inputs are 2-D Tensors: classic matrix multiplication
     * @note - Either input is N-D with N > 2: it is treated as a stack of matrices residing
     * in the last two indexes and broadcast accordingly.
     * @note - First input is 1-D: it is promoted to a matrix by prepending a 1 to its
     * dimensions (D) -> (1,D). The prepended 1 is removed after computation.
     * @note - Second input is 1-D: it is promoted to a matrix by appending a 1 to its
     * dimensions (D) -> (D,1). The appended 1 is removed after computation.
     */
Houssem ROUIS's avatar
Houssem ROUIS committed
    void computeOutputDims() override final;
    void setBackend(const std::string& name, DeviceIdx_t device = 0) override final {
Maxence Naud's avatar
Maxence Naud committed
        mImpl = Registrar<MatMul_Op>::create(name)(*this);
        mOutputs[0]->setBackend(name, device);
Cyril Moineau's avatar
Cyril Moineau committed
    }

    static const std::vector<std::string> getInputsName() {
Houssem ROUIS's avatar
Houssem ROUIS committed
        return {"data_input1", "data_input2"};
    static const std::vector<std::string> getOutputsName() {
Cyril Moineau's avatar
Cyril Moineau committed
};

Houssem ROUIS's avatar
Houssem ROUIS committed
inline std::shared_ptr<Node> MatMul(const std::string& name = "") {
    return std::make_shared<Node>(std::make_shared<MatMul_Op>(), name);
Cyril Moineau's avatar
Cyril Moineau committed
}
} // namespace Aidge

#endif /* AIDGE_CORE_OPERATOR__MATMUL_H_ */