/******************************************************************************** * 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_CONVERT_H_ #define AIDGE_CORE_OPERATOR_CONVERT_H_ #include <cassert> #include <memory> #include <vector> #include "aidge/utils/Registrar.hpp" #include "aidge/operator/OperatorTensor.hpp" #include "aidge/backend/OperatorImpl.hpp" #include "aidge/data/Tensor.hpp" #include "aidge/graph/Node.hpp" #include "aidge/utils/Types.h" namespace Aidge { class Convert_Op : public OperatorTensor, public Registrable<Convert_Op, std::tuple<std::string, std::string>, std::unique_ptr<OperatorImpl>(const Convert_Op&)> { public: static constexpr const char* Type = "Convert"; Convert_Op() : OperatorTensor(Type, 1, 0, 1) {} /** * @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. */ Convert_Op(const Convert_Op& op) : OperatorTensor(op) { mImpl = op.mImpl ? Registrar<Convert_Op>::create({mInputs[0]->getImpl()->backend(), mOutputs[0]->getImpl()->backend()})(*this) : nullptr; } /** * @brief Clone the operator using its copy-constructor. * @see Operator::Convert_Op */ std::shared_ptr<Operator> clone() const override { return std::make_shared<Convert_Op>(*this); } void setBackend(const std::string& name, int device = 0) override { if (mInputs[0]->getImpl() && Registrar<Convert_Op>::exists({mInputs[0]->getImpl()->backend(), name})) { mImpl = Registrar<Convert_Op>::create({mInputs[0]->getImpl()->backend(), name})(*this); } mOutputs[0]->setBackend(name, device); } void forward() override; static const std::vector<std::string> getInputsName(){ return {"data_input"}; } static const std::vector<std::string> getOutputsName(){ return {"data_output"}; } private: /// @brief Store the input data to the output device, before type conversion. /// Used only when there is both a change of device AND of data type. /// Otherwise, data is either directly copied from the other device or /// casted on the same device (requiring a single copy). std::shared_ptr<Tensor> mMovedInput; }; inline std::shared_ptr<Node> Convert(const std::string& name = "") { return std::make_shared<Node>(std::make_shared<Convert_Op>(), name); } } #endif /* AIDGE_CORE_OPERATOR_CONVERT_H_ */