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

#include <cstddef>  // std::size_t
#include <vector>
#include <memory>

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

//Caution: This operator is now deprecated and should no longer be used. 
//It has been replaced by the MetaOperator "Quantizer" (located directly in aidge_quantization).

namespace Aidge {
enum class ScalingAttr {
    ScalingFactor, QuantizedNbBits, IsOutputUnsigned
};

class Scaling_Op
    : public OperatorTensor,
      public Registrable<Scaling_Op, std::string, std::function<std::shared_ptr<OperatorImpl>(const Scaling_Op&)>> {
public:
    static const std::string Type;

private:
    using Attributes_ = StaticAttributes<ScalingAttr, float, std::size_t, bool>;
    template <ScalingAttr e> using attr = typename Attributes_::template attr<e>;
    const std::shared_ptr<Attributes_> mAttributes;

public:
    Scaling_Op() = delete;

    Scaling_Op(float scalingFactor, std::size_t nbBits, bool isOutputUnsigned);

    /**
     * @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.
     */
    Scaling_Op(const Scaling_Op& op);

    /**
     * @brief Clone the operator using its copy-constructor.
     * @see Operator::Scaling_Op
     */
    std::shared_ptr<Operator> clone() const override;

    void setBackend(const std::string& name, DeviceIdx_t device = 0) override final;
    std::set<std::string> getAvailableBackends() const override;

    inline std::shared_ptr<Attributes> attributes() const override { return mAttributes; }
    inline float& scalingFactor() const noexcept { return mAttributes -> getAttr<ScalingAttr::ScalingFactor>(); }
    inline std::size_t& quantizedNbBits() const noexcept { return mAttributes -> getAttr<ScalingAttr::QuantizedNbBits>(); }
    inline bool& isOutputUnsigned() const noexcept { return mAttributes -> getAttr<ScalingAttr::IsOutputUnsigned>(); }

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

/*
inline std::shared_ptr<Node> Scaling(float scalingFactor = 1.0f, const std::string& name = "") {
    return std::make_shared<Node>(std::make_shared<Scaling_Op>(scalingFactor), name);
}
*/
std::shared_ptr<Node> Scaling(float scalingFactor = 1.0f,
                                     std::size_t quantizedNbBits=8,
                                     bool isOutputUnsigned=true,
                                     const std::string& name = "");
} // namespace Aidge

namespace {
template <>
const char* const EnumStrings<Aidge::ScalingAttr>::data[]
    = {"scaling_factor", "quantized_nb_bits", "is_output_unsigned"};
}

#endif /* AIDGE_CORE_OPERATOR_SCALING_H_ */