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

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

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

namespace Aidge {
enum class ReduceMeanAttr { Axes, KeepDims };

template <DimIdx_t DIM>
class ReduceMean_Op : public OperatorTensor,
                public Registrable<ReduceMean_Op<DIM>, std::string, std::unique_ptr<OperatorImpl>(const ReduceMean_Op<DIM> &)>,
                public StaticAttributes<ReduceMeanAttr, std::array<int, DIM>, DimSize_t> {

   public:
    static const std::string Type;

    ReduceMean_Op() = delete;

    using Attributes_ = StaticAttributes<ReduceMeanAttr, std::array<int, DIM>, DimSize_t>;
    template <ReduceMeanAttr e>
    using attr = typename Attributes_::template attr<e>;

    constexpr ReduceMean_Op(const std::array<int, DIM> &axes, DimSize_t keep_dims)
        : OperatorTensor(Type, 1, 0, 1),
          Attributes_(attr<ReduceMeanAttr::Axes>(axes),
                      attr<ReduceMeanAttr::KeepDims>(keep_dims)) {}

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

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

    void computeOutputDims() override final {
        if (!getInput(0)->empty()) {
            std::vector<DimSize_t> outDims;
            for(std::size_t d=0; d<getInput(0)->dims().size(); ++d)
            {
                bool reducedDim =  false;
                for(std::size_t i=0; i<DIM; ++i)
                {
                    int axis_ = this->template getAttr<ReduceMeanAttr::Axes>()[i];
                    std::size_t axis= axis_>=0? axis_: axis_ + getInput(0)->nbDims();
                    if(axis == d)
                    {
                        reducedDim = true;
                        break;
                    }
                }
                if(reducedDim)
                {
                    if(this->template getAttr<ReduceMeanAttr::KeepDims>())
                        outDims.push_back(1);
                }
                else
                    outDims.push_back(getInput(0)->dims()[d]);
            }
            if(outDims.size()>0)
                mOutputs[0]->resize(outDims);
            else
                mOutputs[0]->resize({1});
        }
    }

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

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

template <std::array<DimSize_t, 1>::size_type DIM>
inline std::shared_ptr<Node> ReduceMean(const std::array<int, DIM> &axes,
                                        DimSize_t keep_dims=1,
                                        const std::string& name = "") {
    // FIXME: properly handle default w&b initialization in every cases
    static_assert(DIM<=MaxDim,"Too many kernel dimensions required by ReduceMean, not supported");
    return std::make_shared<Node>(std::make_shared<ReduceMean_Op<static_cast<DimIdx_t>(DIM)>>(axes, keep_dims), name);

}

// helper with C-style array instead of std::array for kernel_dims to allow automatic template DIM deduction
template <DimSize_t DIM>
inline std::shared_ptr<Node> ReduceMean(
    int const (&axes)[DIM],
    DimSize_t keep_dims = 1,
    const std::string& name = "") {
    static_assert(DIM<=MaxDim,"Too many kernel dimensions required by ReduceMean, not supported");
    return ReduceMean(to_array(axes), keep_dims, name);
}

template <DimIdx_t DIM>
const std::string ReduceMean_Op<DIM>::Type = "ReduceMean";

}  // namespace Aidge

namespace {
template <>
const char *const EnumStrings<Aidge::ReduceMeanAttr>::data[] = {"Axes", "KeepDims"};
}

#endif /* AIDGE_CORE_OPERATOR_REDUCEMEAN_H_ */