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

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

#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"
#include "aidge/utils/Types.h"

namespace Aidge {
enum class PadAttr { BeginEndBorders, BorderType, BorderValue };
enum class PadBorderType { Constant, Replicate, Reflect, Wrap };

template <DimIdx_t DIM>
class Pad_Op : public Operator,
                public Registrable<Pad_Op<DIM>, std::string, std::unique_ptr<OperatorImpl>(const Pad_Op<DIM> &)>,
                public StaticAttributes<PadAttr,
                                       std::array<std::array<DimSize_t, 2>, DIM>,
                                       PadBorderType,
                                       double> {
private:
    // 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 = "Pad";

    Pad_Op() = delete;

    using Attributes_ = StaticAttributes<PadAttr,
                                             std::array<std::array<DimSize_t, 2>, DIM>,
                                             PadBorderType,
                                             double>;
    template <PadAttr e>
    using attr = typename Attributes_::template attr<e>;

    constexpr Pad_Op(const std::array<std::array<DimSize_t, 2>, DIM> &beginEndTuples,
                     const PadBorderType &borderType = PadBorderType::Constant,
                     double borderValue = 0.0)
        : Operator(Type),
          Attributes_(attr<PadAttr::BeginEndBorders>(beginEndTuples),
                           attr<PadAttr::BorderType>(borderType),
                           attr<PadAttr::BorderValue>(borderValue)) {
        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.
     */
    Pad_Op(const Pad_Op& op)